如何基于枚举数组的组合来验证JSON模式?

时间:2020-07-14 17:45:59

标签: javascript node.js jsonschema json-schema-validator ajv

给出:

说我正在定义联系人的架构。但是,我可以有“主要联系人”,“学生”或两者兼而有之;以及所有三个选择都具有的不同属性。联系人类型在contact_type: [ "Primary Contact", "Student" ]数组中定义,可以为一个或两个。

说每个联系人类型的字段都是这样的

  • 如果是主要联系人,那么我想要电话号码
  • 如果是学生,那么我想要名字
  • 如果是学生和主要联系人,那么我想要电话号码和名字

用法

我使用Ajv库通过以下代码在Node.js中进行验证:

function validator(json_schema){
    const Ajv = require('ajv');
    const ajv = new Ajv({allErrors: true});
    return ajv.compile(json_schema)
}

const validate = validator(json_schema);

const valid = validate(input);

console.log(!!valid); //true or false
console.log(validate.errors)// object or null

注意:在使用anyOf时,我在allErrors: true上遇到了麻烦,我使用allErrors的输出将所有缺少的/无效的字段返回给用户,而不是返回问题一次。参考:https://github.com/ajv-validator/ajv/issues/980

架构

我已经编写了以下模式,并且如果我执行“ Student”或“ Primary Contact”,它都可以工作,但是当我同时通过这两个模式时,它仍然想针对[“ Student”]或[“ Primary Contact”]进行验证两者。

 {
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "required": [],
  "properties": {},
  "allOf": [
    {
      "if": {
        "properties": {
          "contact_type": {
            "contains": {
              "allOf": [
                {
                  "type": "string",
                  "const": "Primary Contact"
                },
                {
                  "type": "string",
                  "const": "Student"
                }
              ]
            }
          }
        }
      },
      "then": {
        "additionalProperties": false,
        "properties": {
          "contact_type": {
            "type": "array",
            "items": [
              {
                "type": "string",
                "enum": [
                  "Student",
                  "Primary Contact"
                ]
              }
            ]
          },
          "phone": {
            "type": "string"
          },
          "first_name": {
            "type": "string"
          }
        },
        "required": [
          "phone",
          "first_name"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "contact_type": {
            "contains": {
              "type": "string",
              "const": "Student"
            }
          }
        }
      },
      "then": {
        "additionalProperties": false,
        "properties": {
          "contact_type": {
            "type": "array",
            "items": [
              {
                "type": "string",
                "enum": [
                  "Student",
                  "Primary Contact"
                ]
              }
            ]
          },
          "first_name": {
            "type": "string"
          }
        },
        "required": [
          "first_name"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "contact_type": {
            "contains": {
              "type": "string",
              "const": "Primary Contact"
            }
          }
        }
      },
      "then": {
        "additionalProperties": false,
        "properties": {
          "contact_type": {
            "type": "array",
            "items": [
              {
                "type": "string",
                "enum": [
                  "Student",
                  "Primary Contact"
                ]
              }
            ]
          },
          "phone": {
            "type": "string"
          }
        },
        "required": [
          "phone"
        ]
      }
    }
  ]
}

有效输入示例:

  • 仅用于[“主要联系人”]:
    {
        "contact_type":["Primary Contact"],
        "phone":"something"
    }
  • 仅针对[“学生”]:
    {
        "contact_type":["Student"],
        "first_name":"something"
    }
  • 对于[“主要联系人”,“学生”]
    {
        "contact_type":["Primary Contact", "Student"],
        "phone":"something",
        "first_name":"something"
    }

问题:

即使allErrors: true,我也想对此进行验证吗?如果没有,应该如何更改架构?

脚语

除非最后一招,否则我不想将“ contact_type”更改为数组。 (这是一项要求,但只有在没有其他方法的情况下才能打破)

我不允许任何其他项,因此尽管contact_type很常见,但是我在if语句中完全定义了每个对象。如果我将contact_type移出,则会收到有关将contact_type作为AdditionalItem传递的错误消息(它查看if语句的属性,而取出它到普通位置时看不到contact_type)。这就是为什么我的初始properties对象为空的原因。

1 个答案:

答案 0 :(得分:1)

以下是解决验证问题的方法:https://jsonschema.dev/s/XLSDB

这是架构... (如果您想分心,那会更容易)

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",

首先,我们要定义条件检查子方案...

  "definitions": {
    "is_student": {
      "properties": {
        "contact_type": {
          "contains": {
            "const": "Student"
          }
        }
      }
    },
    "is_primay_contact": {
      "properties": {
        "contact_type": {
          "contains": {
            "const": "Primary Contact"
          }
        }
      }
    }
  },

接下来,我假设您一直想要contact_type

  "required": ["contact_type"],
  "properties": {
    "contact_type": {
      "type": "array",
      "items": {
        "enum": ["Primary Contact", "Student"]
      }
    },

我们需要定义所有允许的属性,以防止出现其他属性。 (draft-07不能“透视”诸如allOf之类的涂药器关键字。您可以使用草稿2019-09及其他版本,但这是另一回事了。

    "phone": true,
    "first_name": true
  },
  "additionalProperties": false,

现在,我们需要定义我们的结构约束...

  "allOf": [
    {

如果联系人是学生,则需要名字。

      "if": { "$ref": "#/definitions/is_student" },
      "then": { "required": ["first_name"] }
    },
    {

如果联系人是主要联系人,则需要电话。

      "if": { "$ref": "#/definitions/is_primay_contact" },
      "then": { "required": ["phone"] }
    },
    {

但是,如果联系人既是学生又是主要联系人...

      "if": {
        "allOf": [
          { "$ref": "#/definitions/is_student" },
          { "$ref": "#/definitions/is_primay_contact" }
        ]
      },

然后我们需要电话和名字...

      "then": {
        "required": ["phone", "first_name"]
      },

否则,电话或名字中的一个很好(上一节已覆盖)

      "else": {
        "oneOf": [
          {
            "required": ["phone"]
          },
          {
            "required": ["first_name"]
          }
        ]
      }
    }
  ]
 }

我不认为这是最干净的方法,但是它确实可以满足您提供的要求。

关于获得验证错误,您可以将其传递回END用户...鉴于您提出的条件要求,对于纯JSON Schema来说这不是您期望的...

话虽如此,但ajv确实提供了添加自定义错误消息的扩展名,考虑到我将验证分解为关注点的方式,ajv确实可以用来添加自定义错误({{3 }}。

相关问题