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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ajv-merge-patch

v5.0.1

Published

$merge and $patch keywords for Ajv JSON-Schema validator to extend schemas

Downloads

211,092

Readme

ajv-merge-patch

$merge and $patch keywords for Ajv JSON-Schema validator to extend JSON-schemas

build npm version Coverage Status

Problem solved

The keywords $merge and $patch allow to extend the JSON-schemas using patches in the format JSON Merge Patch (RFC 7396) or JSON Patch (RFC 6902).

Schema extension is necessary if you want to add additional properties to the recursive schema (e.g. meta-schema). Consider this example:

Original schema:

{
  "id": "mySchema.json#",
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "$ref": "#" }
  },
  "additionalProperties": false
}

Valid data: { foo: 'a' }, { foo: 'a', bar: { foo: 'b' } } etc.

If you want to define schema that would allow more properties, the only way to do it without $merge or $patch keywords is to copy-paste and edit the original schema.

Using $merge keyword you can create an extended schema in this way:

{
  "id": "mySchemaExtended.json#",
  "$merge": {
    "source": { "$ref": "mySchema.json#" },
    "with": {
      "properties": {
        "baz": { "type": "number" }
      }
    }
  }
}

Valid data: { foo: 'a', baz: 1 }, { foo: 'a', baz: 1, bar: { foo: 'b', baz: 2 } }, etc.

$merge is implemented as a custom macro keyword using json-merge-patch package.

The same schema extension using $patch keyword:

{
  "id": "mySchemaExtended.json#",
  "$patch": {
    "source": { "$ref": "mySchema.json#" },
    "with": [
      {
        "op": "add",
        "path": "/properties/baz",
        "value": { "type": "number" }
      }
    ]
  }
}

$patch is implemented as a custom macro keyword using fast-json-patch package.

In the majority of cases $merge format is easier to understand and to maintain. $patch can be used for extensions and changes that cannot be expressed using $merge, e.g. Adding an array value.

with property in keywords can also be a reference to a part of some schema, in which case the resolved value will be used rather than the actual object with property $ref.

Please note:

  1. In case the source schema or the patch in with keyword use $ref, they won't take into account the resolution scope for their internal $refs defined by the parent objects - they will be used as separate objects.
  2. $ref in both source schema and with patch take into account current $ref resolution scope (from version 2.0.0).

See also:

Usage with Ajv

These keywords are compatible with Ajv version >=5.1.0-beta.0.

To add these keywords to Ajv instance:

var Ajv = require('ajv');
var ajv = new Ajv();
require('ajv-merge-patch')(ajv);

Using in the browser

You can include these keywords in your code using browserify.

To include only $merge keyword:

require('ajv-merge-patch/keywords/merge')(ajv);

To include only $patch keyword:

require('ajv-merge-patch/keywords/patch')(ajv);

License

MIT