I'm trying to validate the date property of a json object in python.
# Define JSON object
json_object = {
"date": "21ducn23-dsfsd-ds",
"age": 30
}
# Define JSON schema with date format
json_schema = {
"type": "object",
"properties": {
"date": {"type": "string", "format": "date"},
"age": {"type": "integer"},
},
"required": ["date", "age"],
}
}
# Validate JSON object against schema
jsonschema.validate(instance=json_object, schema=json_schema)
But as long as the date field is a string the validation comes through successfully.
I have read several answers to similar questions suggesting that format is annotation only. It seems that the documentation suggests otherwise but I may be missing something.
What can I do to get this to work without defining a regex expression for the date property?
I'm using jsonschema==4.21.1 and jsonschema-specifications==2023.12.1
From the documentation you linked:
To do that in Python using
jsonschema, you need to hook aformat-checking objectinto theValidator.So just add
format_checker=jsonschema.FormatChecker()to yourvalidatecall:It will then raise an error: