validator.js-asserts
Version:
A set of extra asserts for validator.js.
42 lines (30 loc) • 700 B
JavaScript
;
/**
* Module dependencies.
*/
const { Violation } = require('validator.js');
/**
* Export `NullOrDateAssert`.
*/
module.exports = function nullOrDateAssert() {
/**
* Class name.
*/
this.__class__ = 'NullOrDate';
/**
* Validation algorithm.
*/
this.validate = value => {
if (typeof value !== 'string' && value !== null && Object.prototype.toString.call(value) !== '[object Date]') {
throw new Violation(this, value, { value: 'must_be_null_or_a_date' });
}
if (value === null) {
return true;
}
if (isNaN(Date.parse(value)) === true) {
throw new Violation(this, value);
}
return true;
};
return this;
};