Json AJV验证器始终为true,即使使用垃圾输入也是如此
我有三个模式Json AJV验证器始终为true,即使使用垃圾输入也是如此,json,ajv,Json,Ajv,我有三个模式 { "$id": "app.schema.json", "$schema": "http://json-schema.org/draft-07/schema#", "title": "app", "description": "Root configuration object", "properties": { "views" : { "description": "The pages in the application",
{
"$id": "app.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "app",
"description": "Root configuration object",
"properties": {
"views" : {
"description": "The pages in the application",
"type" : "object",
"properties" : {
"summary" : {
"$ref": "view.schema.json"
}
},
"additionalItems": {
"$ref": "view.schema.json"
},
"required" : ["summary"]
}
},
"required": ["views"]
}
我成功地加载了这些文件,并使用addSchema将它们添加到ajv实例中。然而,每当我传递任何数据进行验证时,即使是无效的胡言乱语,validate总是返回true
当我检查实例上的_schemas属性时,所有的模式都存在,并且看起来像它们的json文件定义的那样
使用ajv cli ajv-s schemas/app.schema-r schemas/view.schema-r schemas/component.schema-d plugin.json测试等效程序
由于错误而失败。我将验证程序函数体复制粘贴到IDE中的一个临时文件和一个JSFIDLE中,它在这两种环境中都能工作。在将数据传递到验证函数之前,需要对数据进行JSON解析
{
"$id" : "component.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title" : "component",
"properties": {
"type" : {
"type" : "string",
"enum" : ["container"]
}
},
"anyOf": [
{
"properties": {
"type" : {
"enum" : ["container"]
},
"direction" : {
"enum" : ["horizontal", "vertical"]
}
}
}
]
}
{
"$id" : "view.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title" : "view",
"additionalProperties" : {
"description": "The components in this view",
"$ref" : "component.schema.json"
}
}