# match-json

> A light assertion library built with JSON APIs in mind.

Latest version **1.3.7** (published 2023-07-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install match-json
pnpm add match-json
yarn add match-json
bun add match-json
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.7 |
| Published | 2023-07-22 |
| First published | 2016-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 158 |
| Author | Oscar Menéndez |
| Maintainers | ozkxr |
| Keywords | match, json, api, test, assert, revursive |

## Links

- npm: https://www.npmjs.com/package/match-json
- Repository: https://github.com/ozkxr/match-json
- Homepage: https://github.com/ozkxr/match-json#readme
- Issues: https://github.com/ozkxr/match-json/issues
- npm.io page: https://npm.io/package/match-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.3.7 (latest) — 2023-07-22
- 1.3.6 — 2023-06-19
- 1.3.5 — 2023-01-14
- 1.3.4 — 2022-02-25
- 1.3.3 — 2021-05-05
- 1.3.2 — 2020-06-10
- 1.3.1 — 2020-05-17
- 1.3.0 — 2019-10-13
- 1.2.1 — 2019-10-02
- 1.2.0 — 2019-10-02
- 1.1.1 — 2019-07-14
- 1.0.1 — 2019-07-11
- 0.1.8 — 2019-07-11
- 0.1.7 — 2019-07-11
- 0.1.6 — 2019-07-11
- … 4 more at https://npm.io/package/match-json/versions

## README

# match-json [![Build Status](https://travis-ci.org/ozkxr/match-json.svg?branch=master)](https://travis-ci.org/ozkxr/match)

`match-json` is a light assertion library built with JSON APIs in mind.

JSON API's can only carry JSON types: strings, numbers, booleans, arrays and objects. This library uses that in favor to use functions and regexp to assert JSON API's. You should bring your favorite test library.

## Install

```bash
npm install match-json
```

## Usage

Match JSON Primitives.

```javascript
// Numbers
match(3.1415, 3.1415); // => true

//Strings
match("Uno Dos Tres", "Uno Dos Tres"); // => true

// Booleans
match(false, false); // => true

// And with undefined and null values
match(undefined, undefined); // => true
match(null, null); // => true
```

Match Structures (objects and arrays).

```javascript
match({ name: "Link", color: "green" }, { name: "Link", color: "green" }); // => true
match(["deku", "goron", "zora"], ["deku", "goron", "zora"]); // => true
```

### But the cool part starts here

Matching using Functions

```javascript
match({ name: "Samus" }, (hero) => hero.name.length >= 5); // => true
```

Matching using regular expressions

```javascript
match(
  "fmcloud@nintendo.jp",
  /[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}/
); // => true
```

Also, `match-json` can check JSON types using constructor functions

```javascript
match(5, Number); // => true

match("Hola, mundo", String); // => true

match(false, Boolean); // => true
```

And everything together!

```javascript
match(
  {
    name: { first: "Walter", last: "White" },
    age: 51,
    breakingBad: true,
  },
  {
    name: { first: /[\w]*/, last: "White" },
    age: (age) => age > 18,
    breakingBad: Boolean,
  }
); // => true
```

### Partials

In an object, the default behavior of partial interpret as an error any extra field received. Partial mode ignores potential extra fields received.

Partial mode is enabled by using the `partial` function exposed from `match-json` instead of `match-json` itself. The rest of functionality is not changed.

NOTE: Only objects (and not arrays) are affected by partial mode.

```
import match, { partial } from 'match-json';

// No partial mode
match({ id : 5, name: 'john' }, { id: Number }) // => false

// Partial mode
partial({ id : 5, name: 'john' }, { id: Number }) // => true
```

### Bake

`match-json` also provides a `bake` function that can be used to predefine an expected pattern.

```javascript
const nameIsLarge = match.bake({ name: (name) => name.length > 10 });
nameIsLarge("Tom"); // => false
nameIsLarge("Tooooooooom"); // => true
```

### Signatures

#### Match's signature

- `match( a : T, b : T, partialMode : boolean? ) : boolean`
- `match( a : T, test : RegExp, partialMode : boolean? ) : boolean`
- `match( a : T, test : PredicateFunction, partialMode : boolean? ) : boolean`
- `match( a : T, test : JSONTypeConstructorFunction, partialMode : boolean? ) : boolean`

#### Bake's signature

- `bake( a: T, partialMode : boolean? ) : PredicateFunction`

- ( where PredicateFunction = ( w : T ) : boolean )
- ( where JSONTypeConstructorFunction = Number, String OR Boolean )

## Notes

- Is worth to mention that you only can use JSON-data as the first argument
  of the function. Not functions or RegExp.
- I made this for test my API endpoints, thats why it only accepts to test JSON data.

## Contribution

Feel free to open an issue and/or make a PR if you found a bug or think in a way this lib or even the README can be improved.

## License

MIT

---
_Source: https://npm.io/package/match-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
