- 1.0.0 API Reference
Allows to add custom validation functions.
extension
must be an object, where:
- key is the name of validator
- value is a validation function
Name must consists only of letters.
Validation functions as first argument receive element that is validating. Next arguments can be passed if user specify them.
If validatior name already exists, it will be overwritten (except base ChaiJsonPattern validator).
Example:
import chai from 'chai';
import chaiJsonPattern from 'chai-json-pattern';
const extension = {
customValidator(target) {
// do some stuff
// return boolean
},
};
chaiJsonPattern.extend(extension);
chai.use(chaiJsonPattern);
Example:
import chai from 'chai';
import chaiJsonPattern from 'chai-json-pattern';
chai.use(chaiJsonPattern);
expect({ a: 2 }).to.matchPattern(`{ "a": 2 }`);
To validate objects, you can use JSON format:
const value = {
user: {
id: 12,
name: "Tom",
}
};
expect(value).to.matchPattern(`{
"user": {
"id": 12,
"name": "Tom",
},
}`);
Additionally you can extend validation capabilities by using methods presented below.
By default, all keys have to be included (expect situation, when you will use ...
operator):
Trailing commas in objects are allowed.
Determines if the value is the language type of Object. (e.g. arrays, objects, new Number(0), ...).
const value = {
item: {}
};
expect(value).to.matchPattern(`{
"item": Object,
}`);
Determines if the value is a plain object (object created by the Object constructor or one with a [[Prototype]] of null)
const value = {
item: {}
};
expect(value).to.matchPattern(`{
"item": Object,
}`);
If you want to validate only few keys, than you can use ...
. In result, all unspecified in pattern keys, will be ignored.
const value = {
names: {
id: 12,
test: 'one'
}
};
expect(value).to.matchPattern(`{
"names": {
"test": "one",
...
}
}`);
With ?
you can mark optional key. If the key occurs in target object, it will be validated against provided pattern.
const value = {
names: {
id: 12,
name: "Mark"
}
};
expect(value).to.matchPattern(`{
"names": {
"id": 12,
"name"?: "Mark"
"test"?: "test"
}
}`);
To validate arrays, you can use JSON format:
const value = [
"Mark",
"Tom",
"Robert"
];
expect(value).to.matchPattern(`[
"Mark",
"Tom",
"Robert"
]`);
Additionally you can extend validation capabilities by using methods presented below.
By default, all keys have to be included (expect situation, when you will use ...
operator):
Trailing commas in objects are not allowed.
Determines if the value is classified as an Array object.
const value = {
item: []
};
expect(value).to.matchPattern(`{
"item": Array,
}`);
Specifies the minimum number of items in the array where:
limit
is the lowest number of allowed array items.
const value = {
item: []
};
expect(value).to.matchPattern(`{
"item": Array AND minLength(3),
}`);
Specifies the minimum number of items in the array where:
limit
is the highest number of allowed array items.
const value = {
item: []
};
expect(value).to.matchPattern(`{
"item": Array AND maxLength(7),
}`);
Specifies the exact number of items in the array where:
limit
is number of allowed array items.
const value = {
item: [ 'test' ]
};
expect(value).to.matchPattern(`{
"item": Array AND length(1),
}`);
...
allow to validate n
first or last elements of an array. Rest of array elements will be ignored.
Validating two first elements of array
const value = {
names: [
'John',
'Max',
'Tom'
]
};
expect(value).to.matchPattern(`{
"names": [
'John',
'Max',
...
],
}`);
To validate strings, you can use JSON format:
expect("Test").to.matchPattern("Test");
Additionally you can extend validation capabilities by using provided methods presented below.
Determines if the value is classified as a String primitive or object.
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": String,
}`);
Specifies the minimum number of string characters where:
limit
is the lowest number of allowed string characters.
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": String AND minLength(1),
}`);
Specifies the minimum number of string characters where:
limit
is the highest number of allowed string characters.
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": String AND maxLength(25),
}`);
Specifies the exact number of string characters where:
limit
is number of allowed string characters.
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": String AND length(4),
}`);
Defines a regular expression rule:
pattern
string regular expression the string value must match against
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": String AND regex("/[a-z]+$/"),
}`);
Determines whether the value contain only a-z, A-Z, 0-9 characters
const value = {
item: "book1"
};
expect(value).to.matchPattern(`{
"item": alphanum,
}`);
Determines whether the value is all lowercase.
const value = {
item: "book"
};
expect(value).to.matchPattern(`{
"item": lowercase,
}`);
Determines whether the value is all uppercase.
const value = {
item: "BOX"
};
expect(value).to.matchPattern(`{
"item": uppercase,
}`);
Determines whether the value starts with the given target string.
const value = {
item: "searchbox"
};
expect(value).to.matchPattern(`{
"item": startsWith("search"),
}`);
Determines whether the value ends with the given target string.
const value = {
item: "searchbox"
};
expect(value).to.matchPattern(`{
"item": endsWith("box"),
}`);
Determines whether the value is valid uuid, where:
version
is number between 1-5, specifing acceptable uuid version. If noversion
is specified then all versions are acceptable.
const value = {
id: "2cb07ed8-8e7f-4926-9fa3-6458b5ce14f8"
};
expect(value).to.matchPattern(`{
"id": uuid(4),
}`);
To validate numbers, you can use JSON format:
const value = {
id: 123
};
expect(value).to.matchPattern(`{
"id": 123,
}`);
Additionally you can extend validation capabilities by using methods presented below.
Determines if the value is classified as a Number primitive or object.
const value = {
item: 27.5
};
expect(value).to.matchPattern(`{
"item": Number,
}`);
Determines if the value is an integer.
const value = {
item: 12
};
expect(value).to.matchPattern(`{
"item": Integer,
}`);
Determines if the value is a safe integer. An integer is safe if it's an IEEE-754 double precision number which isn't the result of a rounded unsafe integer.
const value = {
item: 12
};
expect(value).to.matchPattern(`{
"item": SafeInteger,
}`);
Determines if the value is a positive Number.
const value = {
item: 17
};
expect(value).to.matchPattern(`{
"item": positive,
}`);
Determines if the value is a negative Number.
const value = {
item: -12
};
expect(value).to.matchPattern(`{
"item": negative,
}`);
Specifies the minimum value, where:
limit
the minimum allowed value.
const value = {
item: 124
};
expect(value).to.matchPattern(`{
"item": min(999),
}`);
Specifies the maximum value, where:
limit
the maximum allowed value.
const value = {
item: 124
};
expect(value).to.matchPattern(`{
"item": max(999),
}`);
Specifies that the value must be greater than limit
;
const value = {
item: 124
};
expect(value).to.matchPattern(`{
"item": greater(100),
}`);
Specifies that the value must be less than limit
;
const value = {
item: 124
};
expect(value).to.matchPattern(`{
"item": greater(200),
}`);
Specifies that the value must be between start
and up to, but not including, end
. If end is not specified, it's set to start
with start
then set to 0. If start
is greater than end
the params are swapped to support negative ranges.
const value = {
item: 37
};
expect(value).to.matchPattern(`{
"item": range(10, 100),
}`);
To validate booleans, you can use JSON format:
const value = {
test: true,
test2: false
};
expect(value).to.matchPattern(`{
"test": true,
"test2": false,
}`);
Additionally you can extend validation capabilities by using methods presented below.
Determines if the value is classified as a boolean primitive or object.
const value = {
item: true
};
expect(value).to.matchPattern(`{
"item": Boolean,
}`);
Determines if the value is truthy.
const value = {
item: 21
};
expect(value).to.matchPattern(`{
"item": truthy,
}`);
Determines if the value is falsy.
const value = {
item: ''
};
expect(value).to.matchPattern(`{
"item": falsy,
}`);
To validate dates, you can use methods presented below.
Determines if the value is classified as a Date object.
const value = {
item: new Date
};
expect(value).to.matchPattern(`{
"item": Date,
}`);
Determines if the value is parsable into a valid date.
const value = {
created: 'Dec 25, 1995'
updated: 'Wed, 09 Aug 1995 00:00:00 GMT'
};
expect(value).to.matchPattern(`{
created: dateString,
updated: dateString,
}`);
Determines if the value is classified as a Symbol primitive or object.
const value = {
test: Symbol('test')
};
expect(value).to.matchPattern(`{
"test": Symbol,
}`);
Determines if the value matches any data type.
const value = {
test: 17.5
};
expect(value).to.matchPattern(`{
"test": any,
}`);
Determines if the value is null.
const value = {
test: null
};
expect(value).to.matchPattern(`{
"test": null,
}`);
Determines if the value is NaN.
const value = {
test: NaN
};
expect(value).to.matchPattern(`{
"test": NaN,
}`);
Determines if the value is undefined.
const value = {
test: undefined
};
expect(value).to.matchPattern(`{
"test": undefined,
}`);
Determines if the value is null or undefined.
const value = {
test: null
};
expect(value).to.matchPattern(`{
"test": nill,
}`);
Determines if the value is an empty object, collection, map, or set.
const value = {
test: {}
};
expect(value).to.matchPattern(`{
"test": Object AND empty,
}`);
AND
and OR
along with prentices, allows you to create more complex condtions.
const value = {
test: 12,
name: "Mark"
};
expect(value).to.matchPattern(`{
"test": Number AND ( range(10, 20) OR range(40, 50) ),
"name": "Mark" OR "Tom"
}`);