Home > Enterprise >  Error Json Validation against JsonSchema and anyOf
Error Json Validation against JsonSchema and anyOf

Time:01-24

I have a json to validate against jsonSchema, I don't understand why i have an error with the first anyOf, indeed, we can have either a null value, either an array... i set an array but error tells found array instaed of null value??

Why ?

the json to validate

{"simpleTests":[{"attr1":null,"attr2":null,"chiffre":12},{"attr1":"attr1","attr2":"test2"}]}

and the schema

    {
   "$schema":"http://json-schema.org/draft-04/schema#",
   "title":"Simple Test Wrapper",
   "type":"object",
   "additionalProperties":false,
   "properties":{
      "simpleTests":{
         "oneOf":[
            {
               "type":"null",
               "title":"Not included"
            },
            {
               "type":"array",
               "items":{
                  "$ref":"#/definitions/SimpleTest"
               }
            }
         ]
      },
      "attribute1":{
         "oneOf":[
            {
               "type":"null",
               "title":"Not included"
            },
            {
               "type":"string"
            }
         ]
      },
      "number2":{
         "oneOf":[
            {
               "type":"null",
               "title":"Not included"
            },
            {
               "type":"integer"
            }
         ]
      }
   },
   "definitions":{
      "SimpleTest":{
         "type":"object",
         "additionalProperties":false,
         "properties":{
            "attr1":{
               "type":"string"
            },
            "attr2":{
               "oneOf":[
                  {
                     "type":"null",
                     "title":"Not included"
                  },
                  {
                     "type":"string"
                  }
               ]
            },
            "chiffre":{
               "oneOf":[
                  {
                     "type":"null",
                     "title":"Not included"
                  },
                  {
                     "type":"integer"
                  }
               ]
            }
         },
         "required":[
            "attr1"
         ]
      }
   }
}

and the errors

Message:
JSON is valid against no schemas from 'oneOf'.
Schema path:
#/properties/simpleTests/oneOf
Message:
Invalid type. Expected Null but got Array.
Schema path:
#/properties/simpleTests/oneOf/0/type
Message:
Invalid type. Expected String but got Null.
Schema path:
#/definitions/SimpleTest/properties/attr1/type

i don't understand the 2 firsts errors because it is normal... (all code is generated by java code, and validated by java code) here i use

https://www.jsonschemavalidator.net/

to test validation

CodePudding user response:

The results you received only contain the schema location. You might get some better understanding from json-everything.net which will give a bit more comprehensive results, including the instance location.

The primary issue, is at #/simpleTests/0/attr1, which is null, but the subschema at #/properties/simpleTests/oneOf/1/items/$ref/properties/attr1/type only allows strings.

Changing your instance to

{
  "simpleTests":[
    {
      "attr1":"string",   // this is changed
      "attr2":null,
      "chiffre":12
    },
    {
      "attr1":"attr1",
      "attr2":"test2"
    }
  ]
}

As a side note, you can use an array form in type to list multiple types:

"oneOf":[
  {
    "type":"null",
    "title":"Not included"
  },
  {
    "type":"array",
    "items":{
      "$ref":"#/definitions/SimpleTest"
    }
  }
]

can be rewritten as

{
  "type": [ "null", "array" ],
  "items": {
    "$ref":"#/definitions/SimpleTest"
  }
}

CodePudding user response:

you have to fix attribute1 property validation, should add one more

 "properties": {
        "attr1": {
          "oneOf": [
            {
              "type": "null",
              "title": "Not included"
            },
            {
              "type": "string"
            }
          ]
        },
  •  Tags:  
  • Related