【发布时间】:2017-08-06 18:16:29
【问题描述】:
我正在使用 AJV 库来验证我的 JSON 架构。我希望能够验证 Startdate 是一个字符串。如果不是字符串,则应转换为N/A。目前,它仅将undefined 转换为N/A。
但是,在这些情况下,它不会按预期工作:
-
null-> “空” - 0 --> "0"
- 真-->“真”
如果我希望将以上所有内容都转换为 N/A 字符串,我的 customKeyword 函数会是什么样子?
JSON 响应:
jsonResponse: {
"Issue": {
"StartDate": "December 17, 1995 03:24:00"
}
}
架构:
var ajv = new Ajv({
useDefaults: true,
coerceTypes: 'undefined'
});
const schema = {
"type": "object",
"properties": {
"Issue": {
"type": "object",
"properties": {
"StartDate": {
"type": "string"
"default": "N/A",
"stringTypeChecker"
}
}
}
}
}
添加关键字函数:
ajv.addKeyword('stringTypeChecker', {
modifying: true,
validate: function(){
let foo = []
console.log(foo)
}
});
var valid = ajv.validate(schema, jsonResponse);
【问题讨论】:
标签: javascript json validation jsonschema ajv