lx-valid icon indicating copy to clipboard operation
lx-valid copied to clipboard

Error in date format

Open carlosen14 opened this issue 8 years ago • 2 comments

this 9999-99-99 // YYYY-MM-DD passes as a validation format for date

var val = require('lx-valid'),
    objForTest = {
        "date": '9999-99-99'
    },
    schemaForTest = {
        "properties": {
            "date": {
                "type": 'string',
                "required": true,
                "format": 'date'
            }
        }
    },
    result = val.validate(objForTest, schemaForTest);

console.log(result);

carlosen14 avatar Oct 16 '18 17:10 carlosen14

this behavior is correct. lx-valid checks if the string has the format of a date. however it doesn't check if the value itself is a valid date.

sharkboy1976 avatar Oct 17 '18 08:10 sharkboy1976

Well, you could add this validation because you are just passing the work to the user to figure out if it is a valid date..

Solved by using conform

var moment = require('moment')
const validateDateFunc = (value) => {
  let fecha = value.split('-')
  // because moment parse months from 0 - 11 and users from 1 - 12
  let date = moment({ y: parseInt(fecha[0]), M: parseInt(fecha[1]) - 1, d: parseInt(fecha[2]) })
  if (date.isValid()) {
    return true
  }
  return false
}

var val = require('lx-valid'),
    objForTest = {
        "date": '9999-99-99'
    },
    schemaForTest = {
        "properties": {
            "date": {
                "type": 'string',
                "required": true,
                "format": 'date'
                "conform": validateDateFunc
            }
        }
    },
    result = val.validate(objForTest, schemaForTest);

console.log(result);

carlosen14 avatar Oct 17 '18 13:10 carlosen14