Skip to content

Latest commit

 

History

History
95 lines (82 loc) · 4.03 KB

File metadata and controls

95 lines (82 loc) · 4.03 KB

JSON Schema Expression Validator

About


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.


Notification pattern

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

Interpreter evaluates expression using the Visitor Pattern. More details here.

Lexer

Lexer performs the Lexical Analysis of expression.

Parser

Parser parses series of tokens - we map those tokens to terminals in the grammar to figure out could have generated that string.


JSON Schemas

Supported JSON Schemas:

  • ObjectSchema
  • IntegerSchema
  • NumberSchema
  • BooleanSchema
  • ArraySchema
  • StringSchema

Example JSON Schema

{
  "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.

Example expressions:

($.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.


Installation

Run maven install command with mvn clean install

Run maven tests mvn test