emsifa / validasaur

Deno validation library
MIT License
45 stars 9 forks source link

Need dateBefore rule #17

Closed emsifa closed 4 years ago

emsifa commented 4 years ago

Value under this field must be a Date object or a string (that can be parsed using Date.parse() function). This rule accept Date object as argument, and compare that argument with value. If dateFromValue.getTime() >= dateFromArg.getTime() return an invalid object.

Usage example:

const inputs = { value: 'anyvalue' };
const today = new Date();
const [ passes, errors ] = await validate(inputs, {
  value: [isDate, dateBefore(today)],
});

Valid examples:

const [ passes, errors ] = await validate({
  value1: new Date('2020-01-02'), // date object are accepted
  value2: '2020-01-02',           // parseable string also accepted
}, {
  value1: dateBefore(new Date('2020-01-03')), // 2020-01-02 < 2020-01-03
  value2: dateBefore(new Date('2020-01-03')),
});

Invalid examples:

const [ passes, errors ] = await validate({
  value1: new Date('2020-01-02'),
  value2: '20200101',    // this string is not parseable by Date.parse()
  value3: 1596255022145  // number is not accepted even this is valid unix epoch time
}, {
  value1: dateBefore(new Date('2020-01-01')), // 2020-01-02 > 2020-01-01
  value2: dateBefore(new Date('2020-01-01')), // failed because string is not parseable
  value3: dateBefore(new Date('2020-01-01')), // failed because number is not accepted
});