Chai JSON pattern assertions
npm install @iotcomms.io/chai-json-pattern
``js
const pattern =
{
"username": String AND alphanum AND minLength(3) AND maxLength(30),
"password": String AND regex("/^[0-9a-zA-Z]{5,30}$/"),
"access_token"?: String AND Number,
"birthyear": Integer AND range(1900, 2017),
"email": String,
...
};
expect(user).to.matchPattern(pattern);
`user
The above pattern require to be an object with keys that satisfy following conditions:username
* password
* required string
* contain only alphanumeric characters
* must have at least 3 characters, and maximum 30
* access_token
* required string
* must satisfy the custom regex
* birthyear
* optional string or number
*
* required Integer between 1900 and 2017
*
* required string
* allow another keys (e.g. createdAt, updateAt, etc.)
If user will not satisfy pattern, test will not pass, and you will see error with diff between user and pattern.
package.
`
npm install --save-dev chai-json-pattern
`
Then import chaiJsonPattern and use matchPattern:
`js
import chai, { expect } from 'chai';
import chaiJsonPattern from 'chai-json-pattern';chai.use(chaiJsonPattern);
expect({ a: 2 }).to.matchPattern(
{);
``