Package org.eel.kitchen.jsonschema.keyword

Schema validation core elements: keyword validators

See: Description

Package org.eel.kitchen.jsonschema.keyword Description

Schema validation core elements: keyword validators

All keyword validators are built via reflection, since they are dependent on the schema being passed as an argument. Therefore, if you create a keyword validator of yours, you must provide a constructor with a single argument of type JsonNode.

Not only this, but if you do, be sure to pair it with a SyntaxChecker. The principle is that the syntax checker checks that the keyword has a correct shape, which means the keyword validator does not have to check for this. This considerably simplifies the constructor (you only have to do minimal type checking, if any).

To create a new validator, you may either extend KeywordValidator or one of these two specific subclasses:

Here is an example for a proposed minProperties keyword. This keyword validates that an object instance has a minimum number of members. Its argument is a positive integer, we therefore extend PositiveIntegerKeywordValidator directly instead of KeywordValidator:

     public final class MinPropertiesKeywordValidator
         extends PositiveIntegerKeywordValidator
     {
         public MinPropertiesKeywordValidator(final JsonNode schema)
         {
             super("minProperties", schema, NodeType.OBJECT);
         }

         @Override
         public void validateInstance(final ValidationContext context,
             final ValidationReport report, final JsonNode instance)
         {
             if (instance.size() < intValue)
                report.addMessage("object instance has less than "
                + "minProperties elements");
         }
     }
 

Copyright © 2012. All Rights Reserved.