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 🙏

© 2026 – Pkg Stats / Ryan Hefner

assert-json-object

v1.0.0

Published

🧪 Fluent, chainable assertions for JSON structures with dot-path access.

Downloads

4

Readme

assert-json-object

🧪 Fluent, chainable assertions for JSON structures with dot-path access.

License: MIT

✨ Features

  • Assert deeply nested JSON properties using dot-paths (e.g. "foo.bar[0].baz")
  • Type and value assertions
  • Predicate-based assertions
  • Negation (not) support
  • Soft assertion mode: collect all errors instead of throwing immediately
  • TypeScript support
  • 🪶 Zero dependencies

📦 Install

npm install assert-json-object

🚀 Usage

import { assertJson } from "assert-json-object";

const data = {
  user: {
    name: "Alice",
    age: 30,
    tags: ["admin", "editor"],
    address: { city: "NYC" },
  },
};

const assertion = assertJson(data);

assertion
  .toHaveKey("user.name")
  .toBeType("user.age", "number")
  .toMatchValue("user.address.city", "NYC")
  .not.toHaveKey("user.password");
// No error thrown

Soft Assertion Mode

Collect all assertion errors and inspect them later:

const assertion = assertJson(data, { soft: true });

assertion
  .toHaveKey("user.name")
  .toBeType("user.age", "string") // error
  .toMatchValue("user.address.city", "LA") // error
  .not.toHaveKey("user.name"); // error

console.log(assertion.getErrors());

⚙️ API

assertJson(data, options?)

  • data: The JSON object to assert on.
  • options.soft (optional): If true, enables soft assertion mode.

Returns a JsonAssertion instance.

Assertion Methods

| Method | Description | | ------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | | .toHaveKey(path) | Asserts that the given dot-path exists. | | .toBeType(path, type) | Asserts the value at path is of the given type (string, number, boolean, object, array, undefined, null). | | .toMatchValue(path, expected) | Asserts the value at path equals the expected value (deep equality). | | .toSatisfy(path, predicate) | Asserts the value at path satisfies the predicate function. | | .not | Negates the next assertion. | | .getErrors() | Returns an array of errors (only in soft mode). |


✅ Examples

Basic

assertJson({ foo: 1 }).toHaveKey("foo").toBeType("foo", "number");
// No error thrown

Negation

assertJson({ foo: 1 }).not.toHaveKey("bar");
// No error thrown

assertJson({ foo: 1 }).not.toHaveKey("foo");
// Throws: Error: Expected key 'foo' not to exist

Array Indexing

const data = { arr: [{ id: 1 }, { id: 2 }] };
assertJson(data).toHaveKey("arr[1].id").toMatchValue("arr[0].id", 1);
// No error thrown

assertJson(data).toHaveKey("arr[2].id");
// Throws: Error: Expected key 'arr[2].id' to exist

Soft Assertion

const assertion = assertJson({ foo: 1 }, { soft: true });
assertion.toHaveKey("foo").toBeType("foo", "string"); // error

console.log(assertion.getErrors().length);
// Output: 1

console.log(assertion.getErrors()[0].message);
// Output: Expected 'foo' to be type 'string', but got 'number'

🧩 More Examples

Type and Value Checks

const data = { a: 123, b: "hello", c: null, d: [1, 2], e: { x: 1 } };

assertJson(data)
  .toBeType("a", "number")
  .toBeType("b", "string")
  .toBeNull("c")
  .toBeType("d", "array")
  .toBeType("e", "object")
  .toBeDefined("a")
  .toBeTruthy("b")
  .toBeFalsy("c");

Array and String Containment

const data = { arr: [1, 2, 3], str: "hello world" };

assertJson(data).toContainValue("arr", 2).toContainValue("str", "world").not.toContainValue("arr", 5);

Numeric Comparisons

const data = { score: 42 };

assertJson(data)
  .toBeGreaterThan("score", 10)
  .toBeLessThan("score", 100)
  .not.toBeGreaterThan("score", 100)
  .not.toBeLessThan("score", 10);

One Of / Enum

const data = { status: "pending" };

assertJson(data).toBeOneOf("status", ["pending", "done", "failed"]).not.toBeOneOf("status", ["archived"]);

Custom Predicate

const data = { value: 15 };

assertJson(data)
  .toSatisfy("value", (v) => typeof v === "number" && v % 5 === 0)
  .not.toSatisfy("value", (v) => v < 0);

🔎 More Matchers

| Method | Description | | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | | .toHaveKey(path) | Asserts that the given dot-path exists. | | .not.toHaveKey(path) | Asserts that the given dot-path does NOT exist. | | .toBeType(path, type) | Asserts the value at path is of the given type (string, number, boolean, object, array, undefined, null). | | .toBeDefined(path) | Asserts the value at path is defined (not undefined). | | .toBeNull(path) | Asserts the value at path is null. | | .toBeTruthy(path) | Asserts the value at path is truthy. | | .toBeFalsy(path) | Asserts the value at path is falsy. | | .toMatchValue(path, expected) | Asserts the value at path equals the expected value (deep equality). | | .toContainValue(path, value) | Asserts the value at path (array or string) contains the given value. | | .toBeGreaterThan(path, number) | Asserts the value at path is a number greater than the given number. | | .toBeLessThan(path, number) | Asserts the value at path is a number less than the given number. | | .toBeOneOf(path, [values]) | Asserts the value at path matches any value in the provided array (deep equality). | | .toSatisfy(path, predicate) | Asserts the value at path satisfies the predicate function. | | .not | Negates the next assertion. | | .getErrors() | Returns an array of errors (only in soft mode). |


📄 License

MIT © jaktestowac.pl

Powered by jaktestowac.pl team!