npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-json-pattern

v1.1.0

Published

Chai JSON pattern assertions

Downloads

13,223

Readme

Chai JSON Pattern

Deep matching objects with clear, JSON-like syntax.

CircleCI

Introduction

Chai JSON pattern allows you to create blueprints for JavaScript objects to ensure validation of key information. It enables you to use JSON syntax extend with easy to use validators. It came up mostly for testing API with cucumber-js, but can be used in any application. Additionaly you can extend base functionality with custom validators. For more information see API reference.

API

See the detailed API Reference.

Example


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

The above pattern require user to be an object with keys that satisfy following conditions:

  • username
    • required string
    • contain only alphanumeric characters
    • must have at least 3 characters, and maximum 30
  • password
    • required string
    • must satisfy the custom regex
  • access_token
    • optional string or number
  • birthyear
    • required Integer between 1900 and 2017
  • email
    • 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.

Usage

Usage is a two steps process. First, you have to install chai-json-pattern package.

npm run --save-dev chai-json-pattern

Then import chaiJsonPattern and use matchPattern:

import chai, { expect } from 'chai';
import chaiJsonPattern from 'chai-json-pattern';

chai.use(chaiJsonPattern);

expect({ a: 2 }).to.matchPattern(`{
    "a": Number AND range(0, 5),
}`);