Registry / storage / ajv-merge-patch

ajv-merge-patch

JSON →
library5.0.1jsnpmunverified

Custom keywords $merge and $patch for Ajv JSON Schema validator, enabling schema extension via JSON Merge Patch (RFC 7396) and JSON Patch (RFC 6902). Version 5.0.1 supports Ajv >=8.0.0, released sporadically. Unlike base Ajv, these keywords allow dynamic schema composition without manual copy-paste. Used for recursive schemas and meta-schema customization.

npm install ajv-merge-patch
INSTALL
IMPORT
SIG · AJV-MERGE-PATCH
A
ajv-merge-patch
storagejavascriptv5.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const ajvMergePatch = require('ajv-merge-patch'); ajvMergePatch(ajv)
import ajvMergePatch from 'ajv-merge-patch'
Package does not export a default ES module; use CommonJS require or dynamic import().
default
import('ajv-merge-patch').then(m => m.default(ajv))
import ajvMergePatch from 'ajv-merge-patch'
For ES modules, use dynamic import and call .default.
$merge
// Used as JSON Schema keyword; not directly imported
const { $merge } = require('ajv-merge-patch')
$merge is a schema keyword, not an exported symbol.

Demonstrates registering ajv-merge-patch keywords with Ajv, defining a base schema, and extending it via $merge.

const Ajv = require('ajv'); const ajv = new Ajv(); // Register keywords const ajvMergePatch = require('ajv-merge-patch'); ajvMergePatch(ajv); // Define a base schema const baseSchema = { $id: 'base.json', type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }; ajv.addSchema(baseSchema); // Extend schema using $merge const extendedSchema = { $id: 'extended.json', $merge: { source: { $ref: 'base.json' }, with: { properties: { age: { type: 'number' } }, required: ['name', 'age'] } } }; ajv.addSchema(extendedSchema); const validate = ajv.getSchema('extended.json'); const valid = validate({ name: 'John', age: 30 }); console.log(valid); // true console.log(validate.errors);
Debug
Known issues
breakingajv-merge-patch v5 requires Ajv >=8.0.0; not compatible with earlier Ajv versions.
fix
Update Ajv to 8.x or use ajv-merge-patch v4 with Ajv 6-7.
affects: >=5.0.0
deprecated$ref resolution scope may not behave as expected in nested schemas; the source schema and patch are processed as separate objects, ignoring parent resolution scope.
fix
Ensure $ref paths are absolute or adjust schema structure to avoid cross-scope issues.
affects: *
gotchaBoth $merge and $patch keywords resolve $ref in source and with properties, but the resolved values are used as standalone schemas; internal $refs from the original schema may break.
fix
Use absolute $ref URIs or bundle schemas before applying merge/patch.
affects: *
gotchaThe package does not export ES modules; using import statement without bundler may fail.
fix
Use CommonJS require() or dynamic import() with default handling.
affects: *
Errors
Common errors & fixes
Cannot find module 'ajv-merge-patch'
Package not installed or peer dependency version mismatch.
fix
Run 'npm install ajv-merge-patch ajv@>=8.0.0'
TypeError: ajvMergePatch is not a function
Incorrect import: using ES import instead of require.
fix
Use 'const ajvMergePatch = require('ajv-merge-patch');'
Error: unknown keyword "$merge"
Keywords not registered with Ajv instance.
fix
Call 'require('ajv-merge-patch')(ajv)' with your Ajv instance.
ValidationError: data should have required property 'name'
$merge or $patch schema did not merge required fields correctly; source required array is overwritten instead of merged.
fix
Explicitly include required fields in the 'with' patch, or use $patch with add operation.
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies
ajvrequiredpeer dependency required for keyword registration and validation
Agent activity
28 hits · last 30 days
node
24
OpenAI (training)
1
Resources
ajv-merge-patch — npm install ajv-merge-patch · libregistry