2020-01-18 10:13:50 +01:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-12-29 01:27:37 +01:00
|
|
|
import { isArray } from '@ember/array';
|
|
|
|
import { isEmpty } from '@ember/utils';
|
|
|
|
import { assert } from '@ember/debug';
|
2016-01-19 04:56:51 +01:00
|
|
|
import BaseValidator from 'ember-cp-validations/validators/base';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@classic
|
|
|
|
export default class Iso8601Validator extends BaseValidator {
|
2016-02-13 18:37:33 +01:00
|
|
|
validate(value, options = {}) {
|
2018-12-29 01:27:37 +01:00
|
|
|
assert(
|
2016-02-13 18:37:33 +01:00
|
|
|
'options.validFormats must not be set or an array of momentJS format strings',
|
2018-12-29 01:27:37 +01:00
|
|
|
isEmpty(options.validFormats) || isArray(options.validFormats)
|
2016-02-13 18:37:33 +01:00
|
|
|
);
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2016-02-13 18:37:33 +01:00
|
|
|
let valid;
|
2018-12-29 01:27:37 +01:00
|
|
|
const validFormats = isEmpty(options.validFormats) ? ['YYYY-MM-DDTHH:mm:ss.SSSZ'] : options.validFormats;
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2016-01-31 14:54:23 +01:00
|
|
|
if (
|
|
|
|
options.active === false ||
|
|
|
|
(typeof options.active === 'function' && options.active() === false)
|
|
|
|
) {
|
2016-01-19 04:56:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.value = value;
|
|
|
|
|
2016-01-31 14:54:23 +01:00
|
|
|
valid = validFormats.any((validFormat) => {
|
|
|
|
return moment(value, validFormat, true).isValid();
|
|
|
|
});
|
2016-01-19 04:56:51 +01:00
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-02-13 18:37:33 +01:00
|
|
|
return this.createErrorMessage('iso8601', value, options);
|
2016-01-19 04:56:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|