public final class Example9 extends ExampleBase
This example adds a custom keyword with syntax checking and keyword
validation. The chosen keyword is divisors
: it applies to integer
values and takes an array of (unique) integers as an argument. The
validation is the same as for divisibleBy
(or multipleOf
),
except that the result must be zero for all divisors. For instance, if the
value of this keyword is [2, 3]
, then 6 validates successfully but
14 does not (it is divisible by 2 but not 3).
The principle is the same as adding format attributes (see Example8
), with the difference that the keyword is built using the Keyword
class. You need three elements to add a custom keyword:
SyntaxChecker
(optional),KeywordValidator
(optional).Even though you may omit syntax validation, it is not recommended: this means you would need to do type argument checking in the keyword validator constructor. It is all the less recommended that all keyword validators are built using reflection.
The keyword validator must have a single constructor taking a
JsonNode
as an argument (this will be the schema).
Unlike for Example8
, here we choose to augment draft v4 instead
of draft v3, and not making it the default (which means the schema must
have a $schema
member with the appropriate value).
Two sample files are given: the first (link) is valid, the other (link) isn't (the first and third elements fail to divide by one or more factors).
Modifier and Type | Class and Description |
---|---|
static class |
Example9.DivisorsKeywordValidator
Custom keyword validator for
Example9
It must be public because it is built by reflection. |
Constructor and Description |
---|
Example9() |
Modifier and Type | Method and Description |
---|---|
static void |
main(String... args) |
loadResource, printReport
public static void main(String... args) throws IOException
IOException
Copyright © 2013. All Rights Reserved.