简体   繁体   中英

Json schema validation with AND

i was trying to validate this sample json

{
      "wrapper": {
        "contact_number": "1662626872123",
        "dataset": {
          "param": [
            {
              "id": "customer_profile_type",
              "value": "0"
            },
            {
              "id": "sssssssssssssssssss",
              "value": "1"
            }
          ]
        }
      }
    }

am trying to make contact_number required only if array contains id==customer_profile_type AND value==0

i tried

{"wrapper": {
            "if": {
                "properties": {
                    "dataset": {
                        "properties": {
                            "param": {
                                "items": {
                                    "contains": {
                                        "enum": [{ "id": "customer_profile_type","value":"0"}]
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "then": {
                "properties": {
                    "contact_number": {
                        "maxLength": 10
                    }
                }
            }

}

Please help me to fix this

Thanks in advance

You have to use required :

{
  "properties": {
    "wrapper": {
      "if": {
        "properties": {
          "dataset": {
            "properties": {
              "param": {
                "contains": {
                  "properties": {
                    "id": {
                      "enum": [
                        "customer_profile_type"
                      ]
                    },
                    "value": {
                      "enum": [
                        "0"
                      ]
                    }
                  },
                  "required": [
                    "id",
                    "value"
                  ]
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "contact_number": {
            "maxLength": 10
          }
        },
        "required": [
          "contact_number"
        ]
      }
    }
  }
}

You can try this in https://www.jsonschemavalidator.net/s/Co3pwxfs

To better understand JSON Schema read https://json-schema.org/understanding-json-schema/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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