New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-assert

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-assert

check json object matches template

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

JSON Assert

This program checks that a javascript object matches another. The main use case is for use in testing a JSON API.

The difference between this and _.isEqual is that json-assert accepts a function which will run that can perform addition checks. See the examples below.

The difference between this and JSON schema is that json-assert is less verbose, but it is also less flexible.

Installation

npm install json-assert --save

Tests

npm test

Usage

var ja = require('json-assert');

// basic things must match
ja.isEqual({ a: 3}, { b: 4}); // false
ja.isEqual({ a: 3}, { a: 3}); // true

// we don't care what the value is as long as it exists.
ja.isEqual({ a: ja.dontCare }, { a: 3}); // true

// it must exist and match the type (typeof)
ja.isEqual({ a: ja.matchType('string') }, { a: 4}); // false
ja.isEqual({ a: ja.matchType('string') }, { a: "4"}); // true

// we don't care if it exists or not
ja.isEqual({ a: ja.optional }, { a: 4 }); // true
ja.isEqual({ a: ja.optional }, { }); // true

Here is a more realistic example.

var ja = require('json-assert');
var request = require('request');

function testAjax(url, expected) {
  request(url, function (error, response, body) {
    assert(!error);
    assert.equal(response.statusCode, 200);
    assert(ja.isEqual(expected, JSON.parse(body)));
  });
}

testAjax('http://api.test.com', {
  name: "bob",
  age: 45,
  lastLogin: ja.matchType('string')
});

Contributing

Make sure your code:

  • has been run through js beautifier
  • passes js hint
  • has tests written for all new code
  • that all tests pass

Then submit a pull request.

Keywords

json

FAQs

Package last updated on 20 Mar 2015

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