我有一种情况,我需要再次验证json json模式,具体取决于该属性之一(在json模式术语中的enum属性
)中的值。这是json
{
"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' } } ]
它应该首先给出错误,并且应该已经验证了第二种情况,它说它没有枚举值,我在这里做错了吗
答案 0 :(得分:1)
您做对了。 anyOf
关键字表示一个或多个给定的架构必须有效。
它首先检查并发现enum
关键字通过,但是required
关键字失败。因此,该架构失败。
因此,它继续进行下一个模式,发现enum
关键字失败,而required
关键字通过了。因此,该架构也会失败。
anyOf
找不到有效的架构,因此失败,并报告两个架构均未通过验证。
您看到,anyOf
不知道enum
在此架构中具有特殊含义。两种模式都有一个通过的关键字和一个失败的关键字。对于anyOf
,它们是相同的。
这里是一种替代方法,可以为您提供更好的错误消息传递。错误消息最终仍然是非常隐晦的,但是它们集中在问题真正所在的位置。
{
"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"]
}