Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-match-pattern

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-match-pattern

Validates a deep structured JSON pattern

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
157K
decreased by-3.51%
Maintainers
1
Weekly downloads
 
Created

What is chai-match-pattern?

The chai-match-pattern package is a plugin for the Chai assertion library that allows for pattern-based matching of objects. It is particularly useful for testing complex data structures where exact matches are not necessary, and patterns can be used to validate the structure and content of the data.

What are chai-match-pattern's main functionalities?

Pattern Matching

This feature allows you to use lodash predicates to define patterns that the actual data should match. In this example, the actual object is checked against a pattern where 'id' and 'age' should be numbers and 'name' should be a string.

const chai = require('chai');
const chaiMatchPattern = require('chai-match-pattern');
const _ = chaiMatchPattern.getLodashModule();
chai.use(chaiMatchPattern);

const expect = chai.expect;

const actual = {
  id: 123,
  name: 'John Doe',
  age: 30
};

const pattern = {
  id: _.isNumber,
  name: _.isString,
  age: _.isNumber
};

expect(actual).to.matchPattern(pattern);

Partial Matching

Partial matching allows you to specify only a subset of the properties that need to be matched. In this example, the pattern only specifies that 'id' should be a number and 'address.city' should be 'New York', ignoring other properties.

const actual = {
  id: 123,
  name: 'John Doe',
  age: 30,
  address: {
    city: 'New York',
    zip: '10001'
  }
};

const pattern = {
  id: _.isNumber,
  address: {
    city: 'New York'
  }
};

expect(actual).to.matchPattern(pattern);

Custom Pattern Matching

Custom pattern matching allows you to define your own functions to validate the properties. In this example, the 'id' should be greater than 100 and 'name' should start with 'John'.

const actual = {
  id: 123,
  name: 'John Doe',
  age: 30
};

const pattern = {
  id: (val) => val > 100,
  name: (val) => val.startsWith('John')
};

expect(actual).to.matchPattern(pattern);

Other packages similar to chai-match-pattern

Keywords

FAQs

Package last updated on 17 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc