I am currently using jsonschema
with python
to check my json files. I have created a schema.json
that checks the json files that are looped over, and I display the file is invalid or valid according to the checks i have added in my schema.json
.
Currently i am not able to display and highlight the error in the json file which is being checked, for example
"data_type": {
"type": "string",
"not": { "enum": ["null", "0", "NULL", ""] }
},
if the json file has the value "data_type" : "null"
or anything similar in the enum, it will display json file is invalid
but i want it to highlight the error and display you added null in the data_type field
can i do it with jsonschema?
If there is another way without using jsonschema
that will work; the main testing is to check multiple json files in a folder (which can be looped over) and check whether they are valid according to few set of rules and display a clean and nice report which specifically tells us the problem.
CodePudding user response:
What I did is used Draft7Validator
from jsonschema
and it allows to display error message
validator = jsonschema.Draft7Validator(schema)
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
for error in errors:
print(f"{error.message}")
report.write(f"In {file_name}, object {a}, {error.message}{N}")
error_report.write(f"In {file_name}, object {a},{error.json_path[2:]} {error.message}{N}")
This prints -
In test1.json, object 0, apy '' is not of type 'number'
In test2.json, object 0, asset 'asset_contract_address' is a required property
Helpful link that helped me achieve this- Handling Validation Errors