【问题标题】:AJV json schema validation on the basis of particular property data value i.e on enum values基于特定属性数据值(即枚举值)的 AJV json 模式验证
【发布时间】:2018-06-21 18:27:14
【问题描述】:

我有一种情况,我需要再次验证 json json 模式,具体取决于其中一个属性中的值,在 json 模式术语中,枚举属性

这是 json

 { 
  "req":
   {
   user:"",
   company:"",
   dept:"",
   class:""
   reqType:"account"     
   }
 }

reqType 可以采用不同的值,例如 account、dept、classes 基于该字段应该是必需的

我尝试过使用 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' } } ]

它应该只给出第一个错误并且应该验证第二种情况,而不是它说它没有得到枚举值,我在这里做错了什么

【问题讨论】:

    标签: validation jsonschema ajv


    【解决方案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"]
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2017-08-12
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2019-09-14
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多