{"id":15047019,"url":"https://github.com/ajv-validator/ajv-merge-patch","last_synced_at":"2025-08-04T21:33:00.782Z","repository":{"id":43273567,"uuid":"65633716","full_name":"ajv-validator/ajv-merge-patch","owner":"ajv-validator","description":"$merge and $patch keywords for Ajv JSON-Schema validator to extend schemas","archived":false,"fork":false,"pushed_at":"2023-12-12T22:06:48.000Z","size":57,"stargazers_count":47,"open_issues_count":24,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-23T01:25:04.451Z","etag":null,"topics":["ajv","json-merge-patch","json-patch","json-schema","keywords"],"latest_commit_sha":null,"homepage":"https://ajv.js.org","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajv-validator.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"epoberezkin"}},"created_at":"2016-08-13T19:34:21.000Z","updated_at":"2025-06-11T09:11:47.000Z","dependencies_parsed_at":"2024-01-29T10:09:59.462Z","dependency_job_id":"b8bd2edb-dcb2-463d-aecc-d778a05baa4c","html_url":"https://github.com/ajv-validator/ajv-merge-patch","commit_stats":{"total_commits":49,"total_committers":8,"mean_commits":6.125,"dds":"0.36734693877551017","last_synced_commit":"0a4c44f7daa09c6d00ac4abe5ec10c7bc4f6b214"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/ajv-validator/ajv-merge-patch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajv-validator%2Fajv-merge-patch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajv-validator%2Fajv-merge-patch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajv-validator%2Fajv-merge-patch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajv-validator%2Fajv-merge-patch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajv-validator","download_url":"https://codeload.github.com/ajv-validator/ajv-merge-patch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajv-validator%2Fajv-merge-patch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268779067,"owners_count":24306029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajv","json-merge-patch","json-patch","json-schema","keywords"],"created_at":"2024-09-24T20:53:52.822Z","updated_at":"2025-08-04T21:33:00.632Z","avatar_url":"https://github.com/ajv-validator.png","language":"JavaScript","funding_links":["https://github.com/sponsors/epoberezkin"],"categories":[],"sub_categories":[],"readme":"# ajv-merge-patch\n\n$merge and $patch keywords for [Ajv JSON-Schema validator](https://github.com/epoberezkin/ajv) to extend JSON-schemas\n\n[![build](https://github.com/ajv-validator/ajv-merge-patch/actions/workflows/build.yml/badge.svg)](https://github.com/ajv-validator/ajv-merge-patch/actions/workflows/build.yml)\n[![npm version](https://badge.fury.io/js/ajv-merge-patch.svg)](https://www.npmjs.com/package/ajv-merge-patch)\n[![Coverage Status](https://coveralls.io/repos/github/epoberezkin/ajv-merge-patch/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/ajv-merge-patch?branch=master)\n\n\n## Problem solved\n\nThe keywords `$merge` and `$patch` allow to extend the JSON-schemas using patches in the format [JSON Merge Patch (RFC 7396)](https://tools.ietf.org/html/rfc7396) or [JSON Patch (RFC 6902)](https://tools.ietf.org/html/rfc6902).\n\n\nSchema extension is necessary if you want to add additional properties to the recursive schema (e.g. meta-schema). Consider this example:\n\nOriginal schema:\n\n```json\n{\n  \"id\": \"mySchema.json#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"foo\": { \"type\": \"string\" },\n    \"bar\": { \"$ref\": \"#\" }\n  },\n  \"additionalProperties\": false\n}\n```\n\nValid data: `{ foo: 'a' }`, `{ foo: 'a', bar: { foo: 'b' } }` etc.\n\nIf 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.\n\nUsing `$merge` keyword you can create an extended schema in this way:\n\n```json\n{\n  \"id\": \"mySchemaExtended.json#\",\n  \"$merge\": {\n    \"source\": { \"$ref\": \"mySchema.json#\" },\n    \"with\": {\n      \"properties\": {\n        \"baz\": { \"type\": \"number\" }\n      }\n    }\n  }\n}\n```\n\nValid data: `{ foo: 'a', baz: 1 }`, `{ foo: 'a', baz: 1, bar: { foo: 'b', baz: 2 } }`, etc.\n\n`$merge` is implemented as a [custom macro keyword](https://github.com/epoberezkin/ajv/blob/master/CUSTOM.md#define-keyword-with-macro-function) using [json-merge-patch](https://github.com/pierreinglebert/json-merge-patch) package.\n\n\nThe same schema extension using `$patch` keyword:\n\n```json\n{\n  \"id\": \"mySchemaExtended.json#\",\n  \"$patch\": {\n    \"source\": { \"$ref\": \"mySchema.json#\" },\n    \"with\": [\n      {\n        \"op\": \"add\",\n        \"path\": \"/properties/baz\",\n        \"value\": { \"type\": \"number\" }\n      }\n    ]\n  }\n}\n```\n\n`$patch` is implemented as a [custom macro keyword](https://github.com/epoberezkin/ajv/blob/master/CUSTOM.md#define-keyword-with-macro-function) using [fast-json-patch](https://github.com/Starcounter-Jack/JSON-Patch) package.\n\n\nIn 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](https://tools.ietf.org/html/rfc6902#page-18).\n\n`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`.\n\n__Please note__:\n\n1. 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.\n2. `$ref` in both `source` schema and `with` patch take into account current $ref resolution scope (from version 2.0.0).\n\n\nSee also:\n- [v5 proposal](https://github.com/daveclayton/json-schema-validator/wiki/v5:-merge)\n- [$merge and $patch tests](https://github.com/epoberezkin/ajv-merge-patch/blob/master/spec)\n- [discussion in JSON-Schema-Org](https://github.com/json-schema-org/json-schema-spec/issues/15)\n\n\n## Usage with Ajv\n\nThese keywords are compatible with Ajv version \u003e=5.1.0-beta.0.\n\nTo add these keywords to Ajv instance:\n\n```javascript\nvar Ajv = require('ajv');\nvar ajv = new Ajv();\nrequire('ajv-merge-patch')(ajv);\n```\n\n## Using in the browser\n\nYou can include these keywords in your code using browserify.\n\nTo include only `$merge` keyword:\n\n```javascript\nrequire('ajv-merge-patch/keywords/merge')(ajv);\n```\n\nTo include only `$patch` keyword:\n\n```javascript\nrequire('ajv-merge-patch/keywords/patch')(ajv);\n```\n\n## License\n\n[MIT](https://github.com/epoberezkin/ajv-merge-patch/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajv-validator%2Fajv-merge-patch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajv-validator%2Fajv-merge-patch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajv-validator%2Fajv-merge-patch/lists"}