I had the need of displaying a more detailed custom message to the user when an error occurred. I did this by adding a field to the property in the schema and then looking it up on ValidationError.
import json
import jsonschema
def validate(_data, _schema):
try:
jsonschema.validate(_data, _schema)
except jsonschema.ValidationError as e:
return e.schema.get("error_msg", e.message)
schema = {
"title": "index",
"type": "object",
"required": [
"author",
"description"
],
"properties": {
"author": {
"type": "string",
"description": "Name of the Author",
"minLength": 3,
"default": "",
"error_msg": "Please provide a Author name"
},
"description": {
"type": "string",
"description": "Short description",
"minLength": 10,
"default": "",
"error_msg": "Please add a short description"
}
}
}
data = {
"author": "Jerakin",
"description": ""
}
Answer from Jerakin on Stack OverflowVideos
I had the need of displaying a more detailed custom message to the user when an error occurred. I did this by adding a field to the property in the schema and then looking it up on ValidationError.
import json
import jsonschema
def validate(_data, _schema):
try:
jsonschema.validate(_data, _schema)
except jsonschema.ValidationError as e:
return e.schema.get("error_msg", e.message)
schema = {
"title": "index",
"type": "object",
"required": [
"author",
"description"
],
"properties": {
"author": {
"type": "string",
"description": "Name of the Author",
"minLength": 3,
"default": "",
"error_msg": "Please provide a Author name"
},
"description": {
"type": "string",
"description": "Short description",
"minLength": 10,
"default": "",
"error_msg": "Please add a short description"
}
}
}
data = {
"author": "Jerakin",
"description": ""
}
You could catch ValidationError exception and build a custom messages for the specific cases you want from ValidationError metadata. In this object you have:
info of the failed validator: its value and schema path info of the failed instance: its path and value at the time of the failed validation possible errors in subschemas cause (errors caused by a non-validation error)
» pip install jsonschema
» pip install jsonschema-rs
No there is not. You can find the available array validations here: https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4
You can specify that a number must be a multiple of x: https://json-schema.org/understanding-json-schema/reference/numeric.html#multiples but of course this only does part of what you want to achieve.
It is quite simple. I defined a custom validator https://python-jsonschema.readthedocs.io/en/stable/creating/. Unfortunately I have to do validation inside the validator that the instance is an array of numbers.
If you haven't check jsonschema library, it can be useful to validate data. JSON Schema is a way to describe the content of JSON. The library just uses the format to make validations based on the given schema.
I made a simple example from basic usage.
import json
from jsonschema import validate
# Describe what kind of json you expect.
schema = {
"type" : "object",
"properties" : {
"description" : {"type" : "string"},
"status" : {"type" : "boolean"},
"value_a" : {"type" : "number"},
"value_b" : {"type" : "number"},
},
}
# Convert json to python object.
my_json = json.loads('{"description": "Hello world!", "status": true, "value_a": 1, "value_b": 3.14}')
# Validate will raise exception if given json is not
# what is described in schema.
validate(instance=my_json, schema=schema)
# print for debug
print(my_json)
As you're using a JSON file, you can use this example:
import json
def validate(filename):
with open(filename) as file:
try:
data = json.load(file) # put JSON-data to a variable
print("Valid JSON") # in case json is valid
return data
except json.decoder.JSONDecodeError:
print("Invalid JSON") # in case json is invalid