What is a good practise to validate a JavaScript Object which contains date fields?
There are JSON validators like tv4 which can validate the format of strings.
However, our business logic works with dates of instance JavaScript-Date, and these objects won't validate.
Our current procedure is
- Read business object with JSON.parse() using a date reviver
- Process the object with business logic, then validate with
- Convert the object to JSON with a date stringifier
- Read string back with JSON.parse(), now without reviver
- Validate this object
Is there a better way to validate opposed to steps 3, 4 and 5? Preferably validating the business object directly?
Example:
The JSON string
{
"birth": "1994-03-17"
}
Schema for the JSON string
{
type: 'string',
format: 'date-time'
}
The business object
{
birth: new Date("1994-03-17")
}