com.github.fge.jsonschema.examples
Class Example9
java.lang.Object
com.github.fge.jsonschema.examples.ExampleBase
com.github.fge.jsonschema.examples.Example9
public final class Example9
- extends ExampleBase
Ninth example: augmenting schemas with custom keywords
link to source code
link to schema
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:
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).
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Example9
public Example9()
main
public static void main(String... args)
throws IOException,
ProcessingException
- Throws:
IOException
ProcessingException
Copyright © 2014. All Rights Reserved.