java.lang.Object
net.pincette.mongo.Validator
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
$commentoperator and the extension operator$code. The former is for documentation purposes and the latter will be returned in the errors array in thecodefield when the expression returnsfalse. If the value of a field in the expression has an object with only the fieldrefas its value, then the conditions referred to byrefwill 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$existsexpression. - 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
myfieldthen 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis represents a specification with its resolved source.static interfaceThe first argument of the function is the source that has to be resolved. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a validator with extra features for the underlying MongoDB query language.Validator(Features features, Validator.Resolver resolver) Creates a validator with extra features for the underlying MongoDB query language and a resolver. -
Method Summary
Modifier and TypeMethodDescriptionLoads and resolves a validation specification.Loads and resolves a validation specification.Loads and resolves a validation specification.resolve(JsonObject specification) When a validation specification includes other specifications they are resolved recursively.resolve(JsonObject specification, File baseDirectory) When a validation specification includes other specifications they are resolved recursively.resolve(JsonObject specification, String context) When a validation specification includes other specifications they are resolved recursively.static JsonObjectresolve(JsonObject specification, Validator.Resolver resolver, String context) When a validation specification includes other specifications they are resolved recursively.voidsetResolver(Validator.Resolver resolver) Sets the include resolver.Generates a validator with either a filename or a class path resource, in which casesourceshould start with "resource:".validator(JsonObject specification) Generates a validator with the specification.
-
Constructor Details
-
Validator
public Validator() -
Validator
Creates a validator with extra features for the underlying MongoDB query language.- Parameters:
features- the extra features. It may benull.- Since:
- 2.0
-
Validator
Creates a validator with extra features for the underlying MongoDB query language and a resolver.- Parameters:
features- the extra features. It may benull.resolver- the resolver, which resolves includes andreffields. It may benull.- 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 benull.- Returns:
- the resolved validation specification.
- Since:
- 2.2
-
load
Loads and resolves a validation specification.- Parameters:
source- either a filename or a class path resource, in which casesourceshould start with "resource:".- Returns:
- The specification.
- Since:
- 1.4.1
-
load
Loads and resolves a validation specification.- Parameters:
source- either a filename or a class path resource, in which casesourceshould start with "resource:".baseDirectory- the directory to resolve relative file references with. It may benull.- Returns:
- The specification.
- Since:
- 1.4.1
-
load
Loads and resolves a validation specification.- Parameters:
source- either a filename or a class path resource, in which casesourceshould start with "resource:".context- the context to resolve relative references with. It may benull.- Returns:
- The specification.
- Since:
- 2.2
-
resolve
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
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 benull.- Returns:
- the resolved validation specification.
- Since:
- 1.4.1
-
resolve
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 benull.- Returns:
- the resolved validation specification.
- Since:
- 2.2
-
setResolver
Sets the include resolver.- Parameters:
resolver- the given resolver.- Since:
- 2.2.4
-
validator
Generates a validator with either a filename or a class path resource, in which casesourceshould start with "resource:".- Parameters:
source- the validation specification.- Returns:
- An array with the fields
location, which is a JSON pointer, andcode, which is the value of the$codefield in the specification. - Since:
- 1.3
-
validator
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, andcode, which is the value of the$codefield in the specification. - Since:
- 1.4.1
-