lx-valid
lx-valid copied to clipboard
Error in date format
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);
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.
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);