Skip to content

Commit 5f0daf7

Browse files
authored
before/after/onOrBefore/onOrAfter accept function (#281)
The Date validator's before/after/onOrBefore/onOrAfter options now accept a function that returns a date, which is re-evaluated everytime the validator runs. This allows the date validation logic to change in between validations, which is useful for situations where date validation logic is based on date values that may change in between validations.
1 parent 2a703f1 commit 5f0daf7

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,15 @@ Validates the length of a `String` or an `Array`.
197197

198198
#### `date`
199199

200-
This API accepts valid Date objects or a Date in milliseconds since Jan 1 1970. Strings are currently not supported. It is recommended you use use native JavaScript or you library of choice to generate a date from your data.
200+
This API accepts valid Date objects or a Date in milliseconds since Jan 1 1970, or a functiom that returns a Date. Strings are currently not supported. It is recommended you use use native JavaScript or you library of choice to generate a date from your data.
201201

202202
```js
203203
{
204204
propertyName: validateDate({ before: new Date('3000-01-01') }), // must be before 1st Jan. 3000
205205
propertyName: validateDate({ onOrBefore: Date.parse(new Date('3000-01-01')) }), // must be not after 1st Jan. 3000
206206
propertyName: validateDate({ after: new Date('3000-01-01') }), // must be after 1st Jan. 3000
207207
propertyName: validateDate({ onOrAfter: new Date('3000-01-01') }), // must be not before 1st Jan. 3000
208+
propertyName: validateDate({ onOrAfter: () => new Date() }), // must not be in the past
208209
propertyName: validateDate({ onOrAfter: '3000-01-01' }), // Error
209210
}
210211
```

addon/utils/to-date.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
export default function toDate(argument) {
88
const argStr = Object.prototype.toString.call(argument)
99

10+
if (typeof argument === "function") {
11+
argument = argument()
12+
}
13+
1014
if (
1115
argument instanceof Date ||
1216
(typeof argument === 'object' && argStr === '[object Date]')

tests/unit/validators/date-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ module('Unit | Validator | date', function() {
119119
'date is after "before" date'
120120
);
121121

122+
options = { before: () => startDate };
123+
validator = validateDate(options);
124+
assert.equal(
125+
validator(key, afterDate),
126+
buildMessage(key, { afterDate, message: `[BEFORE] date is NOT before ${afterDate}` }),
127+
'before accepts a function that returns a date'
128+
);
129+
122130
options = { before: afterDate };
123131
validator = validateDate(options);
124132
assert.equal(
@@ -145,6 +153,14 @@ module('Unit | Validator | date', function() {
145153
'date is after "onOrBefore" date'
146154
);
147155

156+
options = { onOrBefore: () => startDate };
157+
validator = validateDate(options);
158+
assert.equal(
159+
validator(key, afterDate),
160+
buildMessage(key, { afterDate, message: `[ON OR BEFORE] date is NOT on or before ${afterDate}` }),
161+
'onOrBefore accepts a function that returns a date'
162+
);
163+
148164
options = { onOrBefore: afterDate };
149165
validator = validateDate(options);
150166
assert.equal(
@@ -171,6 +187,14 @@ module('Unit | Validator | date', function() {
171187
'date is after the "after" date'
172188
);
173189

190+
options = { after: () => afterDate };
191+
validator = validateDate(options);
192+
assert.equal(
193+
validator(key, startDate),
194+
buildMessage(key, { startDate, message: `[AFTER] date is NOT after ${startDate}` }),
195+
"after accepts a function that returns a date"
196+
);
197+
174198
options = { after: startDate };
175199
validator = validateDate(options);
176200
assert.equal(
@@ -197,6 +221,14 @@ module('Unit | Validator | date', function() {
197221
'date onOrAfter the "onOrAfter" date is not allowed'
198222
);
199223

224+
options = { onOrAfter: () => onOrAfterDate };
225+
validator = validateDate(options);
226+
assert.equal(
227+
validator(key, startDate),
228+
buildMessage(key, { onOrAfterDate, message: `[ON OR AFTER] date is NOT on or after ${startDate}` }),
229+
'onOrAfter accepts a function that returns a date'
230+
);
231+
200232
options = { onOrAfter: startDate };
201233
validator = validateDate(options);
202234
assert.equal(

0 commit comments

Comments
 (0)