Validation 基于特定属性数据值(即枚举值)的AJV json模式验证
我有一个例子,需要再次验证json json模式,这取决于其中一个属性(在json模式术语中为enum属性)中的值 这里是jsonValidation 基于特定属性数据值(即枚举值)的AJV json模式验证,validation,jsonschema,ajv,Validation,Jsonschema,Ajv,我有一个例子,需要再次验证json json模式,这取决于其中一个属性(在json模式术语中为enum属性)中的值 这里是json { "req": { user:"", company:"", dept:"", class:"" reqType:"account" } } reqType可以采用不同的值,如account、dept、classs 在此基础上,需要一个字段 我曾尝试使用anyOf进行相同的验证,但没有正确验证 例如,
{
"req":
{
user:"",
company:"",
dept:"",
class:""
reqType:"account"
}
}
reqType可以采用不同的值,如account、dept、classs
在此基础上,需要一个字段
我曾尝试使用anyOf进行相同的验证,但没有正确验证
例如,我尝试了下面的模式
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions":
{},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties":
{
"req":
{
"$id": "/properties/req",
"type": "object",
"properties":
{
"user":
{
"$id": "/properties/req/properties/user",
"type": "string",
"title": "The User Schema ",
"default": "",
"examples": [
"a"
]
},
"company":
{
"$id": "/properties/req/properties/company",
"type": "string",
"title": "The Company Schema ",
"default": "",
"examples": [
"b"
]
},
"dept":
{
"$id": "/properties/req/properties/dept",
"type": "string",
"title": "The Dept Schema ",
"default": "",
"examples": [
"c"
]
},
"class":
{
"$id": "/properties/req/properties/class",
"type": "string",
"title": "The Class Schema ",
"default": "",
"examples": [
"d"
]
},
"reqType":
{
"$id": "/properties/req/properties/reqType",
"type": "string",
"title": "The Reqtype Schema ",
"default": "",
"examples": [
"account"
],
"enum": [
"account", "dept", "class"
]
}
},
"required": [
"reqType"
],
"anyOf": [
{
"properties":
{
"reqType":
{
"enum": ["account"]
}
},
"required": ["user", "company"]
},
{
"properties":
{
"reqType":
{
"enum": ["dept"]
}
},
"required": ["dept"]
}]
}
},
"required": [
"req"
]
}
当它满足所有条件时,这似乎工作正常,但当我检查其中一个失败的情况时,它会为其他情况抛出一个错误,如下所示
[ { keyword: 'required',
dataPath: '.req',
schemaPath: '#/properties/req/anyOf/0/required',
params: { missingProperty: 'user' },
message: 'should have required property \'user\'',
schema: [ 'user', 'company' ],
parentSchema: { properties: [Object], required: [Array] },
data: { company: 'b', dept: 'c', class: 'd', reqType: 'account' } },
{ keyword: 'enum',
dataPath: '.req.reqType',
schemaPath: '#/properties/req/anyOf/1/properties/reqType/enum',
params: { allowedValues: [Array] },
message: 'should be equal to one of the allowed values',
schema: [ 'dept' ],
parentSchema: { enum: [Array] },
data: 'account' },
{ keyword: 'anyOf',
dataPath: '.req',
schemaPath: '#/properties/req/anyOf',
params: {},
message: 'should match some schema in anyOf',
schema: [ [Object], [Object] ],
parentSchema:
{ '$id': '/properties/req',
type: 'object',
properties: [Object],
required: [Array],
anyOf: [Array] },
data: { company: 'b', dept: 'c', class: 'd', reqType: 'account' } } ]
它应该只给出第一个错误,并且应该验证第二个案例,而不是说它没有得到枚举值,我在这里做错了吗。
anyOf
关键字意味着一个或多个给定模式必须有效
它检查第一个,发现enum
关键字通过,但required
关键字失败。因此,此模式失败
因此,它转到下一个模式,发现enum
关键字失败,而required
关键字通过。因此,这个模式也失败了
anyOf
未找到有效的架构,因此它失败并报告两个架构均未通过验证
你看,anyOf
不知道enum
在这个模式中有特殊的含义。两个模式都有一个通过的关键字和一个失败的关键字。对于中的任何一个,它们都是相同的
这里有一个替代方案,可以为您提供稍微好一点的错误消息。错误消息最终仍然很神秘,但它们集中在问题真正所在的地方
{
"type": "object",
"properties": {
"req": {
"type": "object",
"properties": {
"reqType": { "enum": ["account", "dept", "class"] }
},
"required": ["reqType"],
"allOf": [
{
"anyOf": [
{
"not": {
"properties": {
"reqType": { "enum": ["account"] }
}
}
},
{ "required": ["user", "company"] }
]
},
{
"anyOf": [
{
"not": {
"properties": {
"reqType": { "enum": ["dept"] }
}
}
},
{ "required": ["dept"] }
]
}
]
}
},
"required": ["req"]
}
你做得对。
anyOf
关键字意味着一个或多个给定模式必须有效
它检查第一个,发现enum
关键字通过,但required
关键字失败。因此,此模式失败
因此,它转到下一个模式,发现enum
关键字失败,而required
关键字通过。因此,这个模式也失败了
anyOf
未找到有效的架构,因此它失败并报告两个架构均未通过验证
你看,anyOf
不知道enum
在这个模式中有特殊的含义。两个模式都有一个通过的关键字和一个失败的关键字。对于中的任何一个,它们都是相同的
这里有一个替代方案,可以为您提供稍微好一点的错误消息。错误消息最终仍然很神秘,但它们集中在问题真正所在的地方
{
"type": "object",
"properties": {
"req": {
"type": "object",
"properties": {
"reqType": { "enum": ["account", "dept", "class"] }
},
"required": ["reqType"],
"allOf": [
{
"anyOf": [
{
"not": {
"properties": {
"reqType": { "enum": ["account"] }
}
}
},
{ "required": ["user", "company"] }
]
},
{
"anyOf": [
{
"not": {
"properties": {
"reqType": { "enum": ["dept"] }
}
}
},
{ "required": ["dept"] }
]
}
]
}
},
"required": ["req"]
}