Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date validator enhancement: before/after/onOrBefore/onOrAfter accept a function that returns a date #281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ Validates the length of a `String` or an `Array`.

#### `date`

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.
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.

```js
{
propertyName: validateDate({ before: new Date('3000-01-01') }), // must be before 1st Jan. 3000
propertyName: validateDate({ onOrBefore: Date.parse(new Date('3000-01-01')) }), // must be not after 1st Jan. 3000
propertyName: validateDate({ after: new Date('3000-01-01') }), // must be after 1st Jan. 3000
propertyName: validateDate({ onOrAfter: new Date('3000-01-01') }), // must be not before 1st Jan. 3000
propertyName: validateDate({ onOrAfter: () => new Date() }), // must not be in the past
propertyName: validateDate({ onOrAfter: '3000-01-01' }), // Error
}
```
Expand Down
4 changes: 4 additions & 0 deletions addon/utils/to-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
export default function toDate(argument) {
const argStr = Object.prototype.toString.call(argument)

if (typeof argument === "function") {
argument = argument()
}

if (
argument instanceof Date ||
(typeof argument === 'object' && argStr === '[object Date]')
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/validators/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ module('Unit | Validator | date', function() {
'date is after "before" date'
);

options = { before: () => startDate };
validator = validateDate(options);
assert.equal(
validator(key, afterDate),
buildMessage(key, { afterDate, message: `[BEFORE] date is NOT before ${afterDate}` }),
'before accepts a function that returns a date'
);

options = { before: afterDate };
validator = validateDate(options);
assert.equal(
Expand All @@ -145,6 +153,14 @@ module('Unit | Validator | date', function() {
'date is after "onOrBefore" date'
);

options = { onOrBefore: () => startDate };
validator = validateDate(options);
assert.equal(
validator(key, afterDate),
buildMessage(key, { afterDate, message: `[ON OR BEFORE] date is NOT on or before ${afterDate}` }),
'onOrBefore accepts a function that returns a date'
);

options = { onOrBefore: afterDate };
validator = validateDate(options);
assert.equal(
Expand All @@ -171,6 +187,14 @@ module('Unit | Validator | date', function() {
'date is after the "after" date'
);

options = { after: () => afterDate };
validator = validateDate(options);
assert.equal(
validator(key, startDate),
buildMessage(key, { startDate, message: `[AFTER] date is NOT after ${startDate}` }),
"after accepts a function that returns a date"
);

options = { after: startDate };
validator = validateDate(options);
assert.equal(
Expand All @@ -197,6 +221,14 @@ module('Unit | Validator | date', function() {
'date onOrAfter the "onOrAfter" date is not allowed'
);

options = { onOrAfter: () => onOrAfterDate };
validator = validateDate(options);
assert.equal(
validator(key, startDate),
buildMessage(key, { onOrAfterDate, message: `[ON OR AFTER] date is NOT on or after ${startDate}` }),
'onOrAfter accepts a function that returns a date'
);

options = { onOrAfter: startDate };
validator = validateDate(options);
assert.equal(
Expand Down