How to return all the jsonschema ValidationError in Python?

Viewed 20

I'm using the jsonschema.ValidationError method to validate my json using a custom json schema. Everything is fine working but the only problem is that ValidationError only displaying the first error and not all of them.

How to display all the errors using this method jsonschema.ValidationError? Thanks in advance. :)

My code is as below

def validate_schema(instance,schema):
    error = []
    try:
        validate(instance=instance,schema=schema)
        print("JSON has been successfully validated.")
    except ValidationError as err:
        print(err)
    #    print("Validation Error:\n" + str(err).split('\n')[0])
        print(str(err).split('\n')[0])

json_schema = {
    "type":"object",
    "properties":{
        "product name": {"type":"boolean"},
        "category_id": {"type":"number"},
        "model_year": {"type":"number"},
        "list_price": {"type":"number"},
        "brand_id": {"type":"number"}
},
}

data = {
        "product_name": 123,
        "category_id": 8,
        "model_year": '2022',
        "list_price": '1495',
        "brand_id": '11'
    }

schema_validator(data,json_schema)
0 Answers
Related