I have an object which contains one or more properties of type date. I would like to validate the object using the ajv json schema validator package. I could convert the properties of type date to a string by using the toISOString(). But the object can be quiet big and thus I dont want to convert all the date-properties of the whole object. Is there a solution other than converting the date to a string? Could I somehow create a custom ajv schema validator?
// My example schema
const schema = {
"properties": {
"createdAt": {
"type": "string",
"format": "date-time"
},
"lastName": { "type": "string" },
"firstName": { "type": "string" }
}
};
// My example testobject
const testObj = {
createdAt: new Date(),
lastName: "Doe",
firstName: "John"
}
// The validation
const validate = ajv.compile(schema);
const valid = validate(testObj);
if(!valid) console.log('Invalid: ' + ajv.errorsText(validate.errors));
This will do a console log, because the testObj.createdAt is a date and not a string.