Skip to content

Latest commit

 

History

History
794 lines (623 loc) · 14.2 KB

API.md

File metadata and controls

794 lines (623 loc) · 14.2 KB

1.0.0 API Reference


extend(extension)

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);

chai matchPattern

Example:

import chai from 'chai';
import chaiJsonPattern from 'chai-json-pattern';

chai.use(chaiJsonPattern);

expect({ a: 2 }).to.matchPattern(`{ "a": 2 }`);

objects validation

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.

Object

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,
}`);

PlainObject

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,
}`);

'...' key

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",
        ...
    }
}`);

"key"?

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"
    }
}`);

arrays validation

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.

Array

Determines if the value is classified as an Array object.

const value = {
    item: []
};

expect(value).to.matchPattern(`{
    "item": Array,
}`);

minLength(limit)

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),
}`);

maxLength(limit)

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),
}`);

length(limit)

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),
}`);

'...' key

... 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',
        ...
    ],
}`);

strings validation

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.

String

Determines if the value is classified as a String primitive or object.

const value = {
    item: "book"
};

expect(value).to.matchPattern(`{
    "item": String,
}`);

minLength(limit)

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),
}`);

maxLength(limit)

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),
}`);

length(limit)

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),
}`);

regex(pattern)

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]+$/"),
}`);

alphanum

Determines whether the value contain only a-z, A-Z, 0-9 characters

const value = {
    item: "book1"
};

expect(value).to.matchPattern(`{
    "item": alphanum,
}`);

lowercase

Determines whether the value is all lowercase.

const value = {
    item: "book"
};

expect(value).to.matchPattern(`{
    "item": lowercase,
}`);

uppercase

Determines whether the value is all uppercase.

const value = {
    item: "BOX"
};

expect(value).to.matchPattern(`{
    "item": uppercase,
}`);

startsWith(target)

Determines whether the value starts with the given target string.

const value = {
    item: "searchbox"
};

expect(value).to.matchPattern(`{
    "item": startsWith("search"),
}`);

endsWith(target)

Determines whether the value ends with the given target string.

const value = {
    item: "searchbox"
};

expect(value).to.matchPattern(`{
    "item": endsWith("box"),
}`);

uuid([version])

Determines whether the value is valid uuid, where:

  • version is number between 1-5, specifing acceptable uuid version. If no version is specified then all versions are acceptable.
const value = {
    id: "2cb07ed8-8e7f-4926-9fa3-6458b5ce14f8"
};

expect(value).to.matchPattern(`{
    "id": uuid(4),
}`);

numbers validation

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.

Number

Determines if the value is classified as a Number primitive or object.

const value = {
    item: 27.5
};

expect(value).to.matchPattern(`{
    "item": Number,
}`);

Integer

Determines if the value is an integer.

const value = {
    item: 12
};

expect(value).to.matchPattern(`{
    "item": Integer,
}`);

SafeInteger

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,
}`);

positive

Determines if the value is a positive Number.

const value = {
    item: 17
};

expect(value).to.matchPattern(`{
    "item": positive,
}`);

negative

Determines if the value is a negative Number.

const value = {
    item: -12
};

expect(value).to.matchPattern(`{
    "item": negative,
}`);

min(limit)

Specifies the minimum value, where:

  • limit the minimum allowed value.
const value = {
    item: 124
};

expect(value).to.matchPattern(`{
    "item": min(999),
}`);

min(limit)

Specifies the maximum value, where:

  • limit the maximum allowed value.
const value = {
    item: 124
};

expect(value).to.matchPattern(`{
    "item": max(999),
}`);

greater(limit)

Specifies that the value must be greater than limit;

const value = {
    item: 124
};

expect(value).to.matchPattern(`{
    "item": greater(100),
}`);

less(limit)

Specifies that the value must be less than limit;

const value = {
    item: 124
};

expect(value).to.matchPattern(`{
    "item": greater(200),
}`);

range(start, end)

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),
}`);

booleans validation

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.

Boolean

Determines if the value is classified as a boolean primitive or object.

const value = {
    item: true
};

expect(value).to.matchPattern(`{
    "item": Boolean,
}`);

truthy

Determines if the value is truthy.

const value = {
    item: 21
};

expect(value).to.matchPattern(`{
    "item": truthy,
}`);

falsy

Determines if the value is falsy.

const value = {
    item: ''
};

expect(value).to.matchPattern(`{
    "item": falsy,
}`);

dates validation

To validate dates, you can use methods presented below.

Date

Determines if the value is classified as a Date object.

const value = {
    item: new Date
};

expect(value).to.matchPattern(`{
    "item": Date,
}`);

dateString

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,
}`);

other

Symbol

Determines if the value is classified as a Symbol primitive or object.

const value = {
    test: Symbol('test')
};

expect(value).to.matchPattern(`{
    "test": Symbol,
}`);

any

Determines if the value matches any data type.

const value = {
    test: 17.5
};

expect(value).to.matchPattern(`{
    "test": any,
}`);

null

Determines if the value is null.

const value = {
    test: null
};

expect(value).to.matchPattern(`{
    "test": null,
}`);

NaN

Determines if the value is NaN.

const value = {
    test: NaN
};

expect(value).to.matchPattern(`{
    "test": NaN,
}`);

undefined

Determines if the value is undefined.

const value = {
    test: undefined
};

expect(value).to.matchPattern(`{
    "test": undefined,
}`);

nill

Determines if the value is null or undefined.

const value = {
    test: null
};

expect(value).to.matchPattern(`{
    "test": nill,
}`);

empty

Determines if the value is an empty object, collection, map, or set.

const value = {
    test: {}
};

expect(value).to.matchPattern(`{
    "test": Object AND empty,
}`);

AND/OR logical operators

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"
}`);