What is the best way to validate a JavaScript Object that includes date fields?
While there are JSON validators such as tv4 that can check the format of string dates, our business logic operates with JavaScript Date instances which these validators do not support.
Our current validation process involves:
- Parsing the business object using JSON.parse() and a custom date reviver
- Executing business logic on the object, then performing validation
- Converting the object back to JSON using a date stringifier
- Re-parsing the string without the reviver
- Validating the final object
Is there a more efficient method to validate the business object directly without going through steps 3, 4, and 5?
For example:
The initial JSON string:
{
"birth": "1994-03-17"
}
The schema for the JSON string:
{
type: 'string',
format: 'date-time'
}
The actual business object:
{
birth: new Date("1994-03-17")
}