Expression Validator is an example application which demonstrates validation of condition expressions. It is a process which scans and validates expressions against JSON Schema in three phases.
-
First phase is a lexical analysis - scanning through the list of characters and group them together into the smallest sequences that represent something. We called them a lexemes.
-
In second phase we try to handle expression which can nest arbitrarily deeply. For that job to be done we must use Context-Free grammar which has its own set of rules. In particular, we're defining an abstract syntax tree (AST). In a parse tree, every single grammar production becomes a node in the tree. We parse a string - a series of tokens - then map those tokens to terminals in the grammar to figure out which rules could have generated that string.
Used grammar:
expression → equality ; equality → comparison ( ( "!=" | "==" ) comparison )* ; comparison → primary ( ( ">" | ">=" | "<" | "<=" ) primary )* ; primary → NUMBER | STRING | BOOLEAN | "nil" | "(" expression ")" ;
-
The third phase is evaluating expression - we recursively interpret each node from previous parsed tree and return boolean value if expression is valid or not.
Instead of throwing exceptions, validation errors are collected using Notification Pattern.
Of course, if validation failed, you are able to fetch validation results using validator.getValidationResult()
method which is a part of the expression validator.
Interpreter evaluates expression using the Visitor Pattern. More details here.
Lexer performs the Lexical Analysis of expression.
Parser parses series of tokens - we map those tokens to terminals in the grammar to figure out could have generated that string.
Supported JSON Schemas:
- ObjectSchema
- IntegerSchema
- NumberSchema
- BooleanSchema
- ArraySchema
- StringSchema
{
"definition": {
"properties": {
"name": {
"validation": {
"required": true
},
"type": "STRING"
},
"age": {
"validation": {
"required": true
},
"type": "INTEGER"
},
"employed": {
"type": "BOOLEAN",
"validation": {
"required": true
}
}
},
"type": "OBJECT"
}
}
Example JSON schema can be found here.
($.age >= 30) || ($.age < 40)
$.name != null
$.name != \"null\"
$.name == \"someCoolName\"
$.employed == false
(($.employed == false) && ($.employed != true))
$.employed != false && $.age > 60
($.age != 30) || ($.age < 40) && ($.employed == true)
More details in Expression Validator Test.
Run maven install command with
mvn clean install
Run maven tests
mvn test