验证器无法识别oneOf中的属性

时间:2019-08-24 13:21:45

标签: jsonschema json-schema-validator

我正在构建一个架构,该架构具有带有枚举的属性,并使用oneOf为枚举的每种情况添加属性和限制。我收到有关绝对不丢失的缺少属性的错误。

模式的简化版本:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://rc2.io/something",
"title": "Rc2 Compute Messages",

"type": "object",
  "minProperties": 2,
"properties": {
    "msg": {
        "type": "string",
        "description": "the command to perform",
        "enum": ["close", "execScript"]
    },
    "argument": {
        "type": "string",
        "description": "main argument for the command"
    }
},
"required": ["msg", "argument"],
"oneOf": [
    {
        "properties": {
            "argument": { "maxLength": 0 },
            "msg": { "const": "close"}
        }
    },
    {
        "properties": {
            "msg": { "const": "execScript"},
            "argument": { "minLength": 1 },
            "queryId": { "type": "number", "multipleOf": 1.0, "minimum": 1 }            },
        "required": ["queryId"]
    }
]
}

我要验证的json:

{
  "queryId:": 2,
  "argument": "testVar<-22",
  "msg": "execScript"
 }

ajv的输出:

[
{
  keyword: 'maxLength',
  dataPath: '.argument',
  schemaPath: '#/oneOf/0/properties/argument/maxLength',
  params: { limit: 0 },
  message: 'should NOT be longer than 0 characters'
},
{
  keyword: 'required',
  dataPath: '',
  schemaPath: '#/oneOf/1/required',
  params: { missingProperty: 'queryId' },
  message: "should have required property 'queryId'"
},
{
  keyword: 'oneOf',
  dataPath: '',
  schemaPath: '#/oneOf',
  params: { passingSchemas: null },
  message: 'should match exactly one schema in oneOf'
}
]

为什么不识别queryId?

1 个答案:

答案 0 :(得分:0)

也许是因为您的JSON数据中存在这种简单的错字“ queryId:”?请注意queryId后的多余冒号。