JSON Schema validation for ISO 8601 date strings
0 reputation · 06 Nov 2021, 11:15 UTC
0 reputation · 06 Nov 2021, 11:15 UTC
JSON lacks a native date data type, necessitating the use of strings or numbers to represent temporal data. While ISO 8601 is the industry standard for string-based date representation, the core JSON specification does not enforce a specific format for these values.
When implementing strict validation using JSON Schema, there is a need to ensure that date strings remain consistent across different language implementations (such as Python's json module and Java's Jackson) without relying on non-standard extensions.
format keyword for dates across different schema versions?Set "type": "string" together with "format": "date" for calendar dates (YYYY-MM-DD) or "format": "date-time" for timestamps. However, the format keyword is an annotation in most JSON Schema drafts unless the validator is explicitly configured to enforce it.
{
"type": "string",
"format": "date"
}For a full timestamp:
{
"type": "string",
"format": "date-time"
}The format keyword has existed since Draft 4 and continues through Drafts 6, 7, 2019-09, and 2020-12. In all of these, format validation is optional by default; the specification does not require validators to reject strings that fail the format assertion.
validateFormat: true (or enable the appropriate format vocabulary in newer drafts).jsonschema: you must attach a FormatChecker to the validator instance; otherwise format is treated as a no-op.date and date-time out of the box, others require a configuration flag or library switch.If you cannot rely on the validator's format support, add a pattern that matches the ISO 8601 shape:
{
"type": "string",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
}Be aware that a regex only checks structure, not calendar validity. A value such as 2026-02-30 may pass the pattern even though it is not a real date. Some validators perform range checks when format enforcement is active, but this is implementation-dependent.
Confirm that your chosen validator actually enforces the constraint. A minimal Python check looks like this:
from jsonschema import validate, FormatChecker
schema = {"type": "string", "format": "date"}
validate("2026-04-15", schema, format_checker=FormatChecker()) # passes
validate("2026-13-01", schema, format_checker=FormatChecker()) # raises ValidationErrorRepeat with "format": "date-time" and a value like "2026-04-15T12:30:45Z" to confirm timezone and fractional-second handling.
If you need the exact configuration flag for a specific validator library, let me know which one you are using.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.