Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→object-no-new-keysnext

object-no-new-keys5.3.0

Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — NONEWKEYS()
  • TWO MODES
  • FOR EXAMPLE
  • COMPETITION
  • Changelog

Installation

Quick Take

Examples

  • Compare each input array item with the reference item at the same index
  • Compare every input array item with the first reference item
  • Report each array position when the reference is not an array

Purpose

Check, does a given plain object have any keys, not present in a reference object. Returns array of zero or more paths in object-path notation.

For example, it can be used to look for any rogue keys in email template content JSON files.

API — noNewKeys()

The main function noNewKeys() is imported like this:

It’s a function which takes three input arguments:

Function’s argumentTypeObligatoryDescription
input
Type: Normally, a plain object or array — but can be whatever in which case the results will be an empty array
Obligatory: yes
inputNormally, a plain object or array — but can be whatever in which case the results will be an empty arrayyesWhat to work upon.
reference
Type: Same, normally, a plain object or array — but can be whatever type in which case result will be empty array
Obligatory: yes
referenceSame, normally, a plain object or array — but can be whatever type in which case result will be empty arrayyesThe reference the input is matched against.
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object.

The Optional Options Object has the following shape:

KeyTypeObligatoryDefaultDescription
mode
Type: Integer number
Obligatory: no
Default: 2
modeInteger numberno2Choose mode: 1 or 2. See below.

Here are all defaults in one place for copying:

The function will return an array of zero or more strings — paths to each key/element in the input which does not exist in reference.

Two modes

This library has two modes:

  1. Strict comparing, having no assumptions about the reference.
  2. Comparing, assuming that the reference will be NORMALISED.

"Normalised“ here means: if any arrays have object children, those objects have the same keys.

These two modes mainly concern the case when both input and reference have an array, but reference has fewer elements and there’s nothing to compare the input element to:

const input = {
  a: [
    {
      // object number 1
      b: "b1",
      c: "c1",
    },
    {
      // object number 2
      b: "b2",
      c: "c2",
      x: "y",
    },
  ],
};

const reference = {
  a: [
    {
      // << just one object!
      b: "b3",
      c: "c3",
    },
  ],
};

First mode will report that a[1].b and a[1].c and a[1].x are all rogue keys, not present in reference.

The second mode will anticipate that reference will be normalised, that is, input array elements can be compared to the first element of an array in reference. The result is the same thing — all objects within an array should have the same keys. This means, input has only one rogue key — a[1].x. And algorithm will identify it by comparing input object a[1] to reference object a[0] — second/third/whatever element in the input to ALWAYS THE FIRST ELEMENT IN REFERENCE, a[0].

We need the second mode, but we give people chance to use the first mode as well. Maybe somebody will find it useful.

For example

import { noNewKeys } from "object-no-new-keys";
const res = noNewKeys(
  {
    a: "a",
    b: "b",
    c: "c",
  },
  {
    c: "z",
  },
);
console.log(JSON.stringify(noNewKeys, null, 4));
// => ['a', 'b']
// meaning, path "a" and path "b" were missing
// path notation uses [] to mark array's contents

works with arrays too:

import { noNewKeys } from "object-no-new-keys";
const res = noNewKeys(
  {
    //<<< input
    a: [
      {
        b: "aaa",
        d: "aaa", // rogue key, record it
        f: "fff", // another rogue key, record it
      },
      {
        c: "aaa",
        k: "kkk", // yet another rogue key, record it
      },
    ],
    x: "x", // rogue too
  },
  {
    // <<< reference
    a: [
      {
        b: "bbb",
        c: "ccc",
      },
      {
        b: "yyy",
        c: "zzz",
      },
    ],
  },
);
console.log(JSON.stringify(res, null, 4));
// => ['a[0].d', 'a[0].f', 'a[1].k', 'x']

Competition

You could try to use a missing-deep-keysopens in a new tab, but it won’t work if your inputs have arrays. For posterity, the algorithm of it is quite wise: run lodash.difference against deep-keysopens in a new tab-flattened stringified key schemas of both object and reference. However, deep-keys does not support arrays, so it’s not that easy.

In short, missing-deep-keys is for cases when you have only objects-within-objects. object-no-new-keys is for work with parsed HTML (AST’s) or JSON.

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab