Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json schema with one mandatory property and at least one another not typed checked

I have this object in json which I would like to validate with a json schema

"reference": {
    "lookup" : "opportunity",
    "shortreference": 93671,
    "guid": "4bb30c46-20ec-e511-9408-005056862bfb"
}

lookup property is mandatory, and then I need at least, either shortreference or guid or both.

{
    "reference": {
      "type": "object",
      "description": "opportunity reference",
      "properties": {
        "lookup": {
          "enum": [
            "employee",
            "opportunity",
            "serviceline",
            "account"
          ]
        }
      },
      "anyOf": [
        {
          "properties": {
            "shortreference": {
              "type": "integer"
            },
            "guid": {
              "type": "string"
            }
          }
        }
      ],
      "required": [
        "lookup"
      ]
    }
  }

EDIT I fixed my issue using the following schema

{
    "reference": {
      "type": "object",
      "required": [
        "lookup"
      ],
      "properties": {
        "lookup": {
          "type": "string",
          "enum" : ["opportunity", "employee", "serviceline", "account"]
        }
      },
      "anyOf": [
        {
          "properties": {
            "shortreference": {
              "type": "integer"
            }
          },
          "required": [
            "shortreference"
          ]
        },
        {
          "properties": {
            "crmguid": {
              "type": "string"
            }
          },
          "required": [
            "crmguid"
          ]
        },
        {
          "properties": {
            "springim": {
              "type": "integer"
            }
          },
          "required": [
            "springim"
          ]
        }
      ]
    }

but when I have both elements, all input type aren't typed checked: if "shortreference" : "12345" (a string rather an integer), as soon as the parameter crmguid is provided, the check about type won't be performed. is there a way to force it. (I'm using AJV : https://github.com/epoberezkin/ajv)

like image 907
camous Avatar asked Apr 26 '16 12:04

camous


People also ask

What is JSON Schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What does Exclusiveminimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).

What is oneOf in JSON Schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.

What does allOf mean in JSON Schema?

By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .


1 Answers

anyOf will stop validation as soon as it finds one schema that matches. It should work as you expect if you pull all of your property declarations into the root of the schema and just have required in the anyOf schemas. That way, all properties will get type checked and anyOf will stop validation when it finds at least one of the required properties.

{
  "reference": {
    "type": "object",
    "required": ["lookup"],
    "properties": {
      "lookup": {
          "type": "string",
          "enum" : ["opportunity", "employee", "serviceline", "account"]
      },
      "shortreference": { "type": "integer" },
      "crmguid": { "type": "string" },
      "springim": { "type": "integer" }
    },
    "anyOf": [
      { "required": ["shortreference"] },
      { "required": ["crmguid"] },
      { "required": ["springim"] }
    ]
  }
}
like image 162
Jason Desrosiers Avatar answered Dec 02 '22 10:12

Jason Desrosiers