jpv
Json Pattern Validator.
it is an easy-to-use JSON schema validator library that validates a JSON object by given the same structural pattern.
Main concept
Verification comes with the jpv.validate function (returns boolean)
jpv | example:
const jpv = ; const json = status : "OK" data : url : "http://example.com" const pattern = status : /^OK$/ data : url : "[url]" jpv // trueInstall
Stable Release (2.2.x)
$ npm install jpv --saveor
$ yarn add jpvRegular Expressions
The easiest and most common way to compare JSON property values is to use a regular expression.
const json = key : "A-8" value: 18 const pattern = key : /^[A-Z]-[0-9]$/ value: /\d/ jpv // truePay attention to the type of regular expression, it should not be a string.
const pattern = key : /^[A-Z]+$/ // -> Right regular expression const pattern = key : '/^[A-Z]+$/' // -> Wrong regular expressionDefined Patterns
The library provides a bunch of various popular defined patterns such as email address, URL, date, etc. There are two way to use defined patterns:
1 - using short tags
key: "[email]" 2 - using "is" operator:
key: jpv | example of using short tag
const json = key: "2017-12-25" const pattern = key: "[date]" jpv // true| example of using "is" oprator
const json = key: "user@gmail.com" const pattern = key: jpv jpv // true| few more examples
const json = key: const pattern = key: "[empty]" const json = key: "some text" const pattern = key: "[min-length(9)]" Library-based available patterns are (with a next to valid examples)
- double - (12.258028)
- naturalNumber - (2)
- number - (0284) - any digital numbers
- integer - (1478)
- url - (https://fb.com)
- alphaNumeric - (a7d34)
- email - (user@example.com)
- date - (2017-05-16)
- datetime - (2017-03-25 10:30:58.235)
- length(3) - (abc) - string length
- min-length(4) - (abcd) - string minimum length
- max-length(5) - (abcde) - string maximum length
- eq(15) - (15) - equal the number
- lt(17) - (16) - less then
- gt(18) - (19) - greater then
- lte(17) - (17) - less then or equal
- gte(18) - (18) - greater then or equal
Functional Pattern
Instead of a patterns, there is also a way to customize using functions.
const json = name: "Mister Albert" const pattern = namelength > 1 jpv // true"and", "or", "not" Logical Operators
The library allows you to create complex patterns using the logical operators: "or", "and", "not":
const json = key: 150 const pattern = key: jpv jpv // true const json = key: 150 const pattern = key: jpv jpv // false “or” and “and” operators arguments take a list of patterns or combined operators
jpvjpvjpv"not" operator takes only one argument (others will be ignored)
jpvUsing logical operators easy to create big and complex conditions like this example:
const and or not = jpv; const jpvEmailOrPhone = ; const json = key1: 'example@gmail.com' key2: '1234567890' const pattern = key1: jpvEmailOrPhone key2: jpvEmailOrPhone jpv // -> true Native Types
This is the case when there is need to validate the value using native JS types. There are two ways to define: 1 - using a brackets short tags.
key: "(any native type name)" 2 - using "typeOf" operator
key: jpv | examples:
const json = key: 98 const pattern = key: "(number)" // or { key: jpv.typeOf('number')} const json = key: false const pattern = key: '(boolean)' const json = key: const pattern = key: jpv Arrays
To validate nested arrays elements all you need is to create one nested pattern inside an array. Each array element will be validated according to the first element pattern.
const json = users : id : 1001478 name : "Alisa" id : 1003476 name : "Bob" const pattern = users : id: "(number)" name: "(string)" jpv // trueExact values
Instead of any pattern also can be set at an exact value. Beware that when setting exact value in pattern and the JSON values must be the same type in order to be valid.
const json = index: 999 jpv // truejpv // falsejpv // true"exact" Operator
There is an "exact" operator which can be useful to clear up confusion when the string looks like a short tag
const json = index: "[email]"const pattern = index: jpv Modes
There are two standard and strict modes.
strict mode is used when your pattern and given JSON should contain exact fields,
while in standard mode - JSON can contain more fields than described in the pattern.
By default JPV validator used standard mode. To set up strict mode - need add 3th argument into the jpv.validate function:
const options = mode: "strict" ; // strict mode jpv;| example of usage strict and standard modes
const json = a: 5789 b: "Another One" const pattern1 = a: "(number)" const pattern2 = a: "(number)" b: "(string)" // standard mode jpv //true // strict mode jpv // false // missing bDebugging
The jpv.validate function returns only a boolean type. But if you want more information in output about errors, just turn on debugging mode ({debug: true}) adding in the third argument of function.
const json = index: "Yes"const pattern = index: "[number]"; // debug modejpv // error - the value of: {"index" = yes} not matched with: "[number]"TypeScript usage
import * as jpv from "jpv";
jpv.validate(json, pattern, false);
Testing
sudo apt install node-tap
sudo npm install tap --dev
tap test/*.js