skip to Main Content

i am using jsonschema validation library of python to perform the schema validation below are the code snippet used along with the schema version and sample json data. my question is when i am using the schema version one where contains keyword is used the error response which i get from code snippet is very generic like it does not gives you the element wise validation failure reason but when i use slightly modified version of schema where i am using items instead of contains the same code snippet gives error in detail at elemenet level.

can someone please explain me, why this is happening or it behaves like this only when using contains keyword or i am missing something in my code snippet.

json schema version one:

{
    "$schema": "http://json-schema.org/draft/2019-09/schema#",
    "type": "array",
    "contains": {
      "type": "number"
    },
    "minContains": 2,
    "maxContains": 5
}

json schema version two:

{
    "$schema": "http://json-schema.org/draft/2019-09/schema#",
    "type": "array",
    "items": {
      "type": "number"
    },
    "minContains": 2,
    "maxContains": 5
  }
  

json data:

["", "1234"]

code snippet:

import json
import jsonschema
from jsonschema import Draft201909Validator
from jsonschema.exceptions import ValidationError


json_file = 'json_schema_validation/sample.json'
json_schema_file = 'json_schema_validation/sample_schemav2.json'


with open(json_file) as f:
    rec = json.load(f)

with open(json_schema_file) as f:
    schema = json.load(f)

  

v = Draft201909Validator(schema)

errors = sorted(v.iter_errors(rec), key=lambda e: e.path)
for error in errors:
    print("---------------------------------------")
    print("json_path list:", list(error.path))
    print("error_message:", error.message)

output when used schema version two:

---------------------------------------
json_path list: [0]
error_message: '' is not of type 'number'
---------------------------------------
json_path list: [1]
error_message: '1234' is not of type 'number'

output when used schema version one:

---------------------------------------
json_path list: []
error_message: ['', '1234'] does not contain items matching the given schema

2

Answers


  1. As per Json Schema documentation,

    Contains means that at least one item of the array is valid

    Items means that all elements of the array are valid

    https://json-schema.org/understanding-json-schema/reference/array

    So it’s up to your use-case to determine if you want to validate at least one or everything

    Login or Signup to reply.
  2. From the JSON Schema reference, regarding to array validation:

    While the items schema must be valid for every item in the array, the contains schema only needs to validate against one or more items in the array.

    There are some examples on the docs where you can see that the items validation restricts the array to have all values compliant with the rule. But in the case of contains, only one valid value is enough to make it compliant.

    This explains why the errors can be detailed in the items particular case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search