JSON Schema: How to check that an array contains at least one object with a property with a given value?

后端 未结 1 774
南方客
南方客 2020-12-17 19:02

How can I check in the following json that at least one element in the array names has a property nickName with the value Ginny?

相关标签:
1条回答
  • 2020-12-17 19:42

    I managed to figure it out using draft-06. On this version a new keyword contains was added. According to this draft specification:

    contains

    The value of this keyword MUST be a valid JSON Schema. An array instance is valid against "contains" if at least one of its elements is valid against the given schema.

    Working schema:

    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "title": "Complex Array",
    
      "type": "object",
      "properties": {
        "names": {
          "type": "array",
          "minItems": 1,
          "contains": {
            "type": "object",
            "properties": {
              "firstName": {
                "type": "string"
              },
              "lastName": {
                "type": "string"
              },
              "nickName": {
                "type": "string",
                "pattern": "^Ginny$"
              }
            },
            "required": ["nickName"]
          },
          "items": {
            "type": "object",
            "properties": {
              "firstName": {
                "type": "string"
              },
              "lastName": {
                "type": "string"
              },
              "nickName": {
                "type": "string"
              }
            }
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题