Format validation
By default, the format
keyword performs no validation (only collects annotations as mandated by the JSON Schema specification). Format validation requires additional dependency:
To enable format validation, attach FormatEvaluatorFactory to your ValidatorFactory
instance:
Supported formats
date, date-time, time - uses
java.time.format.DateTimeFormatter
with standard ISO formatters,duration - uses regex validation as it may be a combination of
java.time.Duration
andjava.time.Period
,email, idn-email - uses
com.sanctionco.jmail.JMail
,hostname - uses regex validation,
idn-hostname - not supported - performs same validation as
hostname
,ipv4, ipv6 - uses
com.sanctionco.jmail.net.InternetProtocolAddress
,uri, uri-reference, iri, iri-reference - uses
java.net.URI
,uuid - uses
java.util.UUID
,uri-template - lenient checking of unclosed braces (compatible with Spring's implementation),
json-pointer, relative-json-pointer - uses manual validation,
regex - uses
java.util.regex.Pattern
.
Vocabulary compliance
Specification defines a way to turn on and off format validation using vocabularies, but due to it being cumbersome to use, it is not the default way to achieve this. This means, if you attach FormatEvaluatorFactory
to your ValidatorFactory
, it will validate formats regardless of the vocabularies' state.
If you still want to use vocabularies, you can use a special FormatEvaluatorFactory
constructor, which accepts a set of strings (vocabulary names):
This will only enable format validation when Vocabulary.Draft2020.FORMAT_ASSERTION
is enabled.
Adding custom formats
Adding custom formats is generally the same as adding custom keywords. See Custom keywords for more information. You can add new formats on top of the default ones by using EvaluatorFactory.compose
or by composition and delegation to FormatEvaluatorFactory
.