在 JSON Schema 中,我正在努力弄清楚 unevaluatedProperties 如何与子条件字段配合使用的行为。 我有一个示例模式,我试图让验证器返回并说明这三个字段不应存在: level1_ShouldNotBeHere field5_ShouldNotBeHere array1_ShouldNotBeHere
{
"required": [
"level1"
],
"properties": {
"level1": {
"type": "object",
"allOf": [
{
"if": {
"properties": {
"field1": {
"enum": [
"field1_value1"
]
}
}
},
"then": {
"properties": {
"field3": {
"type": "string",
"oneOf": [
{
"title": "field3_value1",
"const": "field3_value1"
},
{
"title": "field3_value2",
"const": "field3_value2"
}
]
},
"field4": {
"type": "string"
},
"array_1": {
"minItems": 1,
"items": {
"required": [
"method_array_1",
"detail_array_1"
],
"properties": {
"method_array_1": {
"type": "string",
"oneOf": [
{
"title": "method_array_1_value1",
"const": "method_array_1_value1"
},
{
"title": "method_array_1_value2",
"const": "method_array_1_value2"
}
],
"title": "Method"
},
"detail_array_1": {
"type": "string",
"title": "Detail"
}
},
"type": "object"
},
"type": "array",
"title": "Array 1 Title"
}
}
}
}
],
"properties": {
"field1": {
"type": "string",
"oneOf": [
{
"title": "field1_value1",
"const": "field1_value1"
},
{
"title": "field1_value2",
"const": "field1_value2"
}
]
},
"field2": {
"type": "string"
}
}
}
},
"type": "object",
"title": "Unit"
}
最终,我尝试使用验证器 (AJV) 返回与
unevaluatedProperties
相关的错误列表,并从生成的 json 中删除这些字段。
在此示例中,我可以让前两个字段返回,但是我无法弄清楚将
unevaluatedProperties
放在哪里以使以下字段作为错误返回:array1_ShouldNotBeHere
{
"required": [
"level1"
],
"unevaluatedProperties" : false,
"properties": {
"level1": {
"type": "object",
"allOf": [
{
"if": {
"properties": {
"field1": {
"enum": [
"field1_value1"
]
}
}
},
"then": {
"properties": {
"field3": {
"type": "string",
"oneOf": [
{
"title": "field3_value1",
"const": "field3_value1"
},
{
"title": "field3_value2",
"const": "field3_value2"
}
]
},
"field4": {
"type": "string"
},
"array_1": {
"minItems": 1,
"items": {
"required": [
"method_array_1",
"detail_array_1"
],
"properties": {
"method_array_1": {
"type": "string",
"oneOf": [
{
"title": "method_array_1_value1",
"const": "method_array_1_value1"
},
{
"title": "method_array_1_value2",
"const": "method_array_1_value2"
}
],
"title": "Method"
},
"detail_array_1": {
"type": "string",
"title": "Detail"
}
},
"type": "object"
},
"type": "array",
"title": "Array 1 Title"
}
}
}
}
],
"unevaluatedProperties" : false,
"properties": {
"field1": {
"type": "string",
"oneOf": [
{
"title": "field1_value1",
"const": "field1_value1"
},
{
"title": "field1_value2",
"const": "field1_value2"
}
]
},
"field2": {
"type": "string"
}
}
}
},
"type": "object",
"title": "Unit"
}
这应该完成你所解释的。
unevaluatedProperties
应在您不希望定义其他属性的每个 object
架构位置设置。
在您的示例中,根架构、
level1
架构和array_1
项目架构。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Unit",
"type": "object",
"required": [
"level1"
],
"unevaluatedProperties": false,
"properties": {
"level1": {
"allOf": [
{
"if": {
"properties": {
"field1": {
"const": "field1_value1"
}
}
},
"then": {
"type": "object",
"properties": {
"field3": {
"oneOf": [
{
"title": "field3_value1",
"const": "field3_value1"
},
{
"title": "field3_value2",
"const": "field3_value2"
}
]
},
"field4": {
"type": "string"
},
"array_1": {
"title": "Array 1 Title",
"type": "array",
"minItems": 1,
"items": {
"required": [
"method_array_1",
"detail_array_1"
],
"type": "object",
"unevaluatedProperties": false,
"properties": {
"method_array_1": {
"oneOf": [
{
"title": "method_array_1_value1",
"const": "method_array_1_value1"
},
{
"title": "method_array_1_value2",
"const": "method_array_1_value2"
}
],
"title": "Method"
},
"detail_array_1": {
"title": "Detail",
"type": "string"
}
}
}
}
}
}
}
],
"type": "object",
"unevaluatedProperties": false,
"properties": {
"field1": {
"oneOf": [
{
"title": "field1_value1",
"const": "field1_value1"
},
{
"title": "field1_value2",
"const": "field1_value2"
}
]
},
"field2": {
"type": "string"
}
}
}
}
}
您列出了所需的属性 - 这意味着您必须至少拥有这些属性。但这并不意味着你不能拥有别人。
尝试在架构中将 extraProperties 设置为 false。这样,只允许模式中的属性。
如果我正确理解您的问题,您需要以下内容: https://www.jsonschemavalidator.net/s/6yAGzNwg