简体   繁体   English

使用AJV中的json模式验证对象数组中的每个对象键

[英]Validate each object key in the object array with json schema in ajv

I have an array which includes objects which have different key values. 我有一个数组,其中包含具有不同键值的对象。 I want to validate each object key. 我想验证每个对象键。 For example, age field can get only Equal and Not equal operator values. 例如,age字段只能获取等于和不等于运算符值。 So "op" key is different for each key. 因此,“ op”键对于每个键都是不同的。 For example name should be used with Contains operator. 例如,名称应与Contains运算符一起使用。

[ [

{ age:21, op: "Equal" },
{ name:21, op: "Contains" },
{ date: 1564577662198, op: "Not equal" }

] ]

I have written a schema as, 我写了一个架构,

{
  "title": "ValidatorSchema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "age":{
        "type": "number"
      },
      "operator" : {
        "type" : "string",
        "enum" : ["Equal" , "Not equal"]
      },
      "name":{
        "type": "string"
      },
      "operator" : {
        "type" : "string",
        "enum" : ["Contains"]
      }
     }
  }
}

But i couldn't relate each key with an operator. 但是我无法将每个键与运算符关联。 How can I do it? 我该怎么做?

Here's one way to express the requirements (as best I understand them) using the " JSON Extended Structural Schema " language, JESS: 这是使用“ JSON扩展结构模式 ”语言JESS来表达需求(据我所知)的一种方法:

[
    ["&",
     {"comment": "https://stackoverflow.com/questions/57291339/validate-each-object-key-in-the-object-array-with-json-schema-in-ajv",
      "::<=":  {
                 "age": "number",
                 "operator": "string",
                 "name": "string",
                 "date": "number"
      }
     },
     { "comment": "age field can get only Equal and Not equal operator values",
       "ifcond": { "has": "age"},
       "then":   ["&", {"forall": ".[operator]", "enumeration": ["Equal", "Not equal"]}]
     },
     { "comment": "name should be used with Contains operator",
       "ifcond": { "forall": ".[operator]", "equal": "Contains" },
       "then":   ["&", {"has": "name" } ]
     }
    ]
]

Explanation 说明

A JESS schema is a collection of JSON texts. JESS模式是JSON文本的集合。 Here, one JSON document suffices, as shown above. 此处,一个JSON文档就足够了,如上所示。

The outer square brackets express the requirement that the target JSON entity must be a JSON array. 外部方括号表示要求目标JSON实体必须是JSON数组。

The "&" signifies that what follows is a conjunction of constraints. “&”表示紧随约束。 Here there are three top-level constraints, each with a "comment" field to indicate what's going on. 这里有三个顶级约束,每个约束都有一个“ comment”字段以指示正在发生的情况。 In brief: 简单来说:

  1. The first top-level constraint expresses the requirement that the components of the array must contain objects formed using any of the indicated keys with the type constraint shown. 第一个顶级约束条件表示要求数组的组件必须包含使用任何所示键(带有所示类型约束)形成的对象。

  2. The second top-level constraint expresses the condition that the "age field can get only Equal and Not equal operator values". 第二个顶级约束条件表示“年龄字段只能获得等于和不等于运算符值”的条件。

  3. The third expresses the constraint that if the value of .operator is "Contains" in an object, then that object must have a "name" key (the type of which has already been specified in (1.)). 第三个表示约束,如果对象中.operator的值是“包含”,则该对象必须具有“名称”键(其类型已在(1.)中指定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM