1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
import json
import jsonschema
from jsonschema import validate
from jsonschema import FormatChecker
from jsonschema import Draft6Validator
# Describe what kind of json you expect.
rulesSchema = {
"type": "object",
"anyOf": [{"required": ["AGE", "NAME", "PHONE"]},
{"required": ["AGE", "NAME", "PHONE_CELL"]}],
"properties":{"AGE": {"type": "number",
"minimum": 0,
"maximum": 130,
"multipleOf": 1},
"NAME": {"type": "string"},
"MAIL": {"format": "email"},
"FAVORITE_COLOR": {"type": "string",
"enum": ["red", "blue", "yellow"]
},
"PHONE": {"type": "string", "pattern":"^[0-9]{4}$"},
"CELL": {"type": "string", "pattern":"^[0-9]{4}$"},
},
}
def validateSchema(jsonData):
try:
validate(jsonData,rulesSchema)
except jsonschema.exceptions.ValidationError as err:
print ("Erreur :".format(str(err)))
return False
return True |
Partager