Class Validator

java.lang.Object
net.pincette.mongo.Validator

public class Validator extends Object
With this class you can generate a validator based on a specification in JSON. Use one instance to generate several validators. This way common specifications are loaded and compiled only once. A specification is a JSON document with the following fields, all of which are optional.
conditions
An array of MongoDB query expressions. which can have the standard $comment operator and the extension operator $code. The former is for documentation purposes and the latter will be returned in the errors array in the code field when the expression returns false. If the value of a field in the expression has an object with only the field ref as its value, then the conditions referred to by ref will be applied to the field. If it is an array with such a ref object, then the value is first checked to be an array and the referred conditions will be applied to the objects in the array. When a condition is an expression for a field it will be tested only when that field is present and the condition is not a $exists expression.
description
A short string describing the specification.
include
An array of source strings, which can be filenames or class path resources, which start with "resource:". They refer to other specifications, the contents of which is merged with the including specification. All conditions in the included specifications will be in force and all of their macros are available. The including specification may redefine a macro, but it will only affect itself and other specifications it is included in.
macros
A JSON object where each field represents a macro. If a field is called myfield then you can use it in conditions and other macros as _myfield_. Such an occurrence will be replaced with the value in the macro object.
title
The title of the specification.

This is an example:

{
   "title": "The structure of the select slot command.",
   "include": [
     "resource:/validators/jes/command.json",
     "resource:/validators/active_location.json"
   ],
   "conditions": [
     {
       "_command": "Select"
     },
     {
       "_type": "invitations-slot",
       "$code": "TYPE"
     },
     {
       "_state.free": {
         "$eq": true
       },
       "$comment": "The slot should be free.",
       "$code": "FREE"
     },
     {
       "invitation": {
         "$exists": true
       },
       "$code": "REQUIRED"
     },
     {
       "$expr": {
         "$eq": [
           "$invitation.larsGroup.clbId",
           "$_state.clbId"
         ]
       },
       "$code": "SAME_CLB"
     },
     {
       "_jwt": {
         "ref": "../../jwt.json"
       }
     },
     {
       "clbs": [
         {
           "ref": "clb.json"
         }
       ]
     }
   ]
 }
Since:
1.3
Author:
Werner Donné
See Also:
  • Constructor Details

    • Validator

      public Validator()
    • Validator

      public Validator(Features features)
      Creates a validator with extra features for the underlying MongoDB query language.
      Parameters:
      features - the extra features. It may be null.
      Since:
      2.0
    • Validator

      public Validator(Features features, Validator.Resolver resolver)
      Creates a validator with extra features for the underlying MongoDB query language and a resolver.
      Parameters:
      features - the extra features. It may be null.
      resolver - the resolver, which resolves includes and ref fields. It may be null.
      Since:
      2.2
  • Method Details

    • resolve

      public static JsonObject resolve(JsonObject specification, Validator.Resolver resolver, String context)
      When a validation specification includes other specifications they are resolved recursively.
      Parameters:
      specification - the validation specification.
      resolver - the resolver for resolving all inclusions and references.
      context - the context to resolve relative references with. It may be null.
      Returns:
      the resolved validation specification.
      Since:
      2.2
    • load

      public JsonObject load(String source)
      Loads and resolves a validation specification.
      Parameters:
      source - either a filename or a class path resource, in which case source should start with "resource:".
      Returns:
      The specification.
      Since:
      1.4.1
    • load

      public JsonObject load(String source, File baseDirectory)
      Loads and resolves a validation specification.
      Parameters:
      source - either a filename or a class path resource, in which case source should start with "resource:".
      baseDirectory - the directory to resolve relative file references with. It may be null.
      Returns:
      The specification.
      Since:
      1.4.1
    • load

      public JsonObject load(String source, String context)
      Loads and resolves a validation specification.
      Parameters:
      source - either a filename or a class path resource, in which case source should start with "resource:".
      context - the context to resolve relative references with. It may be null.
      Returns:
      The specification.
      Since:
      2.2
    • resolve

      public JsonObject resolve(JsonObject specification)
      When a validation specification includes other specifications they are resolved recursively.
      Parameters:
      specification - the validation specification.
      Returns:
      the resolved validation specification.
      Since:
      1.4.1
    • resolve

      public JsonObject resolve(JsonObject specification, File baseDirectory)
      When a validation specification includes other specifications they are resolved recursively.
      Parameters:
      specification - the validation specification.
      baseDirectory - the directory to resolve relative file references with. It may be null.
      Returns:
      the resolved validation specification.
      Since:
      1.4.1
    • resolve

      public JsonObject resolve(JsonObject specification, String context)
      When a validation specification includes other specifications they are resolved recursively.
      Parameters:
      specification - the validation specification.
      context - the context to resolve relative references with. It may be null.
      Returns:
      the resolved validation specification.
      Since:
      2.2
    • setResolver

      public void setResolver(Validator.Resolver resolver)
      Sets the include resolver.
      Parameters:
      resolver - the given resolver.
      Since:
      2.2.4
    • validator

      public Function<JsonObject,JsonArray> validator(String source)
      Generates a validator with either a filename or a class path resource, in which case source should start with "resource:".
      Parameters:
      source - the validation specification.
      Returns:
      An array with the fields location, which is a JSON pointer, and code , which is the value of the $code field in the specification.
      Since:
      1.3
    • validator

      public Function<JsonObject,JsonArray> validator(JsonObject specification)
      Generates a validator with the specification. It will be resolved first.
      Parameters:
      specification - the validation specification.
      Returns:
      An array with the fields location, which is a JSON pointer, and code , which is the value of the $code field in the specification.
      Since:
      1.4.1