{"id":15685601,"url":"https://github.com/eomm/json-schema-resolver","last_synced_at":"2025-04-13T10:21:42.097Z","repository":{"id":47469732,"uuid":"262732526","full_name":"Eomm/json-schema-resolver","owner":"Eomm","description":"Resolve all your JSON Schema $refs to relative path","archived":false,"fork":false,"pushed_at":"2024-08-19T12:06:45.000Z","size":26,"stargazers_count":10,"open_issues_count":3,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-23T19:50:56.317Z","etag":null,"topics":["definitions","fastify","json-schema","json-schema-resolver","ref"],"latest_commit_sha":null,"homepage":"","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/Eomm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-10T07:14:28.000Z","updated_at":"2024-10-06T01:50:40.000Z","dependencies_parsed_at":"2024-06-18T17:09:02.583Z","dependency_job_id":"e94000f8-04ab-4af8-a7a7-80fc0040a05c","html_url":"https://github.com/Eomm/json-schema-resolver","commit_stats":{"total_commits":22,"total_committers":3,"mean_commits":7.333333333333333,"dds":0.2272727272727273,"last_synced_commit":"63020a0dbaaac1b088e16e6e17435af8670cd8af"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjson-schema-resolver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjson-schema-resolver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjson-schema-resolver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjson-schema-resolver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/json-schema-resolver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248696232,"owners_count":21147093,"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","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":["definitions","fastify","json-schema","json-schema-resolver","ref"],"created_at":"2024-10-03T17:27:28.079Z","updated_at":"2025-04-13T10:21:42.068Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-schema-resolver\n\n[![CI](https://github.com/Eomm/json-schema-resolver/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Eomm/json-schema-resolver/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/json-schema-resolver.svg?style=flat)](https://www.npmjs.com/package/json-schema-resolver)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nResolve all `$refs` in your [JSON schema](https://json-schema.org/specification.html)!  \nThis module will resolve the `$ref` keyword against the `externalSchemas` you will provide.  \nBy resolving the `$ref` keyword, means that you get back a single BIG inlined JSON schema that does not rely on any external schema.\nIf a reference is missing, it will not throw any error.\n\n\n## Install\n\n```sh\nnpm install json-schema-resolver\n```\n\nThis plugin support Node.js \u003e= 10\n\n## Usage: resolve one schema against external schemas\n\nThe `$ref` string is going to be modified to point to a local reference URI: `#/definitions/\u003cgenerated key\u003e`.\nMoreover, the `definitions` keyword will be decorated with the external schemas to get only one JSON schema resolved as output.\n\nBy default the `\u003cgenerated key\u003e` has the `def-${index}` format.\nYou can customize it by passing a `buildLocalReference` function as follows:\n\n```js\nconst RefResolver = require('json-schema-resolver')\n\nconst ref = RefResolver({\n  clone: true, // Clone the input schema without changing it. Default: false,\n  buildLocalReference (json, baseUri, fragment, i) {\n    // the `json` that is being resolved\n    // the `baseUri` object of the schema. Its values is the parse result from https://www.npmjs.com/package/fast-uri\n    // the `fragment` is the `$ref` string when the `$ref` is a relative reference\n    // the `i` is a local counter to generate a unique key\n    return `def-${i}` // default value\n  }\n})\n\nconst inputSchema = {\n  $id: 'http://example.com/SimplePerson',\n  type: 'object',\n  properties: {\n    name: { type: 'string' },\n    address: { $ref: 'relativeAddress#' },\n    houses: { type: 'array', items: { $ref: 'relativeAddress#' } }\n  }\n}\n\nconst addresSchema = {\n  $id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com\n  type: 'object',\n  properties: {\n    zip: { type: 'string' },\n    city: { type: 'string' }\n  }\n}\n\nconst singleSchema = ref.resolve(inputSchema, { externalSchemas: [addresSchema] })\n// inputSchema is untouched thanks to clone:true\n```\n\n`singleSchema` will be like:\n\n```json\n{\n  \"$id\": \"http://example.com/SimplePerson\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    },\n    \"address\": {\n      \"$ref\": \"#/definitions/def-0\"\n    },\n    \"houses\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"$ref\": \"#/definitions/def-0\"\n      }\n    }\n  },\n  \"definitions\": {\n    \"def-0\": {\n      \"$id\": \"relativeAddress\",\n      \"type\": \"object\",\n      \"properties\": {\n        \"zip\": {\n          \"type\": \"string\"\n        },\n        \"city\": {\n          \"type\": \"string\"\n        }\n      }\n    }\n  }\n}\n```\n\n## Usage: resolve multiple schemas against external shared schemas\n\nWhen you have multiple schemas to resolve against a collection of shared schema you need to use this\nmodule with little changes.\n\nThis is needed to have all the same definitions path (`#/definitions/\u003cgenerated key\u003e`) across all the\nroot schemas\n\n```js\nconst ref = RefResolver({\n  clone: true, // Clone the input schema without changing it. Default: false\n  applicationUri: 'my-application.org', // You need to provide an unique URI to resolve relative `$id`s\n  externalSchemas: [addresSchema] // The schemas provided at the creation of the resolver, will be used evvery time `.resolve` will be called\n})\n\nconst inputSchema = {\n  $id: 'http://example.com/SimplePerson',\n  type: 'object',\n  properties: {\n    name: { type: 'string' },\n    address: { $ref: 'relativeAddress#' },\n    houses: { type: 'array', items: { $ref: 'relativeAddress#' } }\n  }\n}\n\n// the resolved schema DOES NOT have definitions added\nconst singleSchema = ref.resolve(inputSchema)\nconst anotherResolvedSchema = ref.resolve(input_2_Schema) // resolve schemas within the same externalSchemas\n\n// to get the definition you need only to call:\nconst sharedDefinitions = ref.definitions()\n```\n\n## Debug\n\nTo debug this module, simply set:\n\n```bash\nexport DEBUG=json-schema-resolver\n```\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Fjson-schema-resolver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feomm%2Fjson-schema-resolver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Fjson-schema-resolver/lists"}