2016-01-19 04:56:51 +01:00
|
|
|
import BaseValidator from 'ember-cp-validations/validators/base';
|
|
|
|
import moment from 'moment';
|
2016-02-13 18:37:33 +01:00
|
|
|
import Ember from 'ember';
|
2016-01-19 04:56:51 +01:00
|
|
|
|
|
|
|
export default BaseValidator.extend({
|
2016-02-13 18:37:33 +01:00
|
|
|
validate(value, options = {}) {
|
|
|
|
Ember.assert(
|
|
|
|
'options.validFormats must not be set or an array of momentJS format strings',
|
|
|
|
Ember.isEmpty(options.validFormats) || Ember.isArray(options.validFormats)
|
|
|
|
);
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2016-02-13 18:37:33 +01:00
|
|
|
let valid;
|
|
|
|
const validFormats = Ember.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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|