public final class Example9 extends ExampleBase
This example adds a custom keyword with syntax checking, digesting 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 multipleOf
except that it is
restricted to integer values and the instance must be a multiple of 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).
For this, you need to create your own keyword. This is done using Keyword.newBuilder(String)
, where the argument is the name of your keyword,
and then add the following elements:
SyntaxChecker
(using KeywordBuilder.withSyntaxChecker(SyntaxChecker)
;Digester
(using KeywordBuilder.withDigester(Digester)
;KeywordValidator
(using KeywordBuilder.withValidatorClass(Class)
.Then, as in Example8
, you need to get hold of a Library
(we choose again to extend the draft v4 library) and add the (frozen)
keyword to it using LibraryBuilder.addKeyword(Keyword)
.
The keyword validator must have a single constructor taking a
JsonNode
as an argument (which will be the result of the Digester
). Note that you may omit to write a digester and choose instead to
use an IdentityDigester
or a SimpleDigester
(which you inject
into a keyword using KeywordBuilder.withIdentityDigester(NodeType, NodeType...)
and KeywordBuilder.withSimpleDigester(NodeType, NodeType...)
respectively).
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, com.github.fge.jsonschema.exceptions.ProcessingException
IOException
com.github.fge.jsonschema.exceptions.ProcessingException
Copyright © 2014. All Rights Reserved.