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

Add an option to display date in UTC in date validators #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions addon/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
* @param {Any} value
* @param {Object} options
* @param {Boolean} options.allowBlank If true, skips validation if the value is empty
* @param {Boolean} options.displayUTC If true, displays date in UTC mode in error message
* @param {String} options.before The specified date must be before this date
* @param {String} options.onOrBefore The specified date must be on or before this date
* @param {String} options.after The specified date must be after this date
Expand All @@ -44,7 +45,7 @@ const {
*/
export default function validateDate(value, options) {
let errorFormat = getWithDefault(options, 'errorFormat', 'MMM Do, YYYY');
let { format, precision, allowBlank } = getProperties(options, ['format', 'precision', 'allowBlank']);
let { format, precision, allowBlank, displayUTC } = getProperties(options, ['format', 'precision', 'allowBlank', 'displayUTC']);
let { before, onOrBefore, after, onOrAfter } = getProperties(options, ['before', 'onOrBefore', 'after', 'onOrAfter']);
let date;

Expand All @@ -53,43 +54,43 @@ export default function validateDate(value, options) {
}

if (format) {
date = parseDate(value, format, true);
date = parseDate(value, format, true, displayUTC);
if (!date.isValid()) {
return validationError('wrongDateFormat', value, options);
}
} else {
date = parseDate(value);
date = parseDate(value, undefined, false, displayUTC);
if (!date.isValid()) {
return validationError('date', value, options);
}
}

if (before) {
before = parseDate(before, format);
before = parseDate(before, format, false, displayUTC);
if (!date.isBefore(before, precision)) {
set(options, 'before', before.format(errorFormat));
return validationError('before', value, options);
}
}

if (onOrBefore) {
onOrBefore = parseDate(onOrBefore, format);
onOrBefore = parseDate(onOrBefore, format, false, displayUTC);
if (!date.isSameOrBefore(onOrBefore, precision)) {
set(options, 'onOrBefore', onOrBefore.format(errorFormat));
return validationError('onOrBefore', value, options);
}
}

if (after) {
after = parseDate(after, format);
after = parseDate(after, format, false, displayUTC);
if (!date.isAfter(after, precision)) {
set(options, 'after', after.format(errorFormat));
return validationError('after', value, options);
}
}

if (onOrAfter) {
onOrAfter = parseDate(onOrAfter, format);
onOrAfter = parseDate(onOrAfter, format, false, displayUTC);
if (!date.isSameOrAfter(onOrAfter, precision)) {
set(options, 'onOrAfter', onOrAfter.format(errorFormat));
return validationError('onOrAfter', value, options);
Expand All @@ -99,10 +100,14 @@ export default function validateDate(value, options) {
return true;
}

export function parseDate(date, format, useStrict = false) {
export function parseDate(date, format, useStrict = false, displayUTC = false) {
if (date === 'now' || isEmpty(date)) {
return moment();
return displayUTC ? moment.parseZone() : moment();
}

return isNone(format) ? moment(new Date(date)) : moment(date, format, useStrict);
if (isNone(format)) {
return displayUTC ? moment.parseZone(date) : moment(date);
}

return displayUTC ? moment.parseZone(date, format, useStrict) : moment(date, format, useStrict);
}
21 changes: 21 additions & 0 deletions tests/unit/validators/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,24 @@ test('after or on precision', function(assert) {
}
}
});

test('display message with date in UTC mode', function(assert) {
assert.expect(3);

result = validate('1/1/2015', {
before: '2014-01-01T00:00+08:00'
});
assert.equal(processResult(result), 'This field must be before Dec 31st, 2013', 'By default, format the date considering the timezone');

result = validate('1/1/2016', {
displayUTC: false,
before: '2014-01-01T00:00+08:00'
});
assert.equal(processResult(result), 'This field must be before Dec 31st, 2013', 'Format the date considering the timezone');

result = validate('1/1/2017', {
displayUTC: true,
before: '2014-01-01T00:00+08:00'
});
assert.equal(processResult(result), 'This field must be before Jan 1st, 2014', 'Format the date in UTC mode');
});