format : String
format
StringDefine and assert semantic information about a string instance.
The format
keyword restricts string instances to the given logical type and
produces an annotation value.
However, this vocabulary is not used by default in the JSON Schema 2020-12
dialect. To use it, a custom dialect that includes this vocabulary is required.
As a consequence, not many JSON Schema implementations support it. In most
cases, it is advised to stick to the Format-Annotation
variant of this keyword.
Best Practice
While technically allowed by the JSON Schema specification, extending this keyword with custom formats is considered to be an anti-pattern that can introduce interoperability issues and undefined behavior. As a best practice, stick to standardised formats. If needed, introduce a new keyword for custom string logical types.
The supported formats are the following:
Remember that JSON Schema is a constraint-driven language.
Therefore, non-string instances successfully validate against this
keyword. If needed, make use of the type
keyword to constraint
the accepted type accordingly.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/my-dialect",
"$dynamicAnchor": "meta",
"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/format-assertion": true
},
"allOf": [
{ "$ref": "https://json-schema.org/draft/2020-12/meta/core" },
{ "$ref": "https://json-schema.org/draft/2020-12/meta/format-assertion" }
]
}
{
"$schema": "https://example.com/custom-meta-schema",
"format": "email"
}
"john.doe@example.com"
{ "keyword": "/format", "instance": "", "value": "email" }
"foo-bar"
45