I'm using AJV library to validate my JSON schema. I want to be able to validate Startdate
to be a string. In the case where it is not a string, it should be converted to N/A
. Currently, it only converts undefined
to N/A
.
However, in these cases it does not work as expected:
null
-> "null"- 0 --> "0"
- true --> "true"
If I want all of the above to be converted to an N/A
string, what would my customKeyword function look like?
JSON response:
jsonResponse: {
"Issue": {
"StartDate": "December 17, 1995 03:24:00"
}
}
schema:
var ajv = new Ajv({
useDefaults: true,
coerceTypes: 'undefined'
});
const schema = {
"type": "object",
"properties": {
"Issue": {
"type": "object",
"properties": {
"StartDate": {
"type": "string"
"default": "N/A",
"stringTypeChecker"
}
}
}
}
}
addKeyword function:
ajv.addKeyword('stringTypeChecker', {
modifying: true,
validate: function(){
let foo = []
console.log(foo)
}
});
var valid = ajv.validate(schema, jsonResponse);