|
| 1 | +# JSON Schema $Ref Parser |
| 2 | + |
| 3 | +#### Parse, Resolve, and Dereference JSON Schema $ref pointers |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install using [npm](https://docs.npmjs.com/about-npm/): |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @hey-api/json-schema-ref-parser |
| 11 | +yarn add @hey-api/json-schema-ref-parser |
| 12 | +bun add @hey-api/json-schema-ref-parser |
| 13 | +``` |
| 14 | + |
| 15 | +## The Problem: |
| 16 | + |
| 17 | +You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead |
| 18 | +of time. Maybe you don't. Maybe some are local files, and others are remote URLs. Maybe they are a mix of JSON and YAML |
| 19 | +format. Maybe some of the files contain cross-references to each other. |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "definitions": { |
| 24 | + "person": { |
| 25 | + // references an external file |
| 26 | + "$ref": "schemas/people/Bruce-Wayne.json" |
| 27 | + }, |
| 28 | + "place": { |
| 29 | + // references a sub-schema in an external file |
| 30 | + "$ref": "schemas/places.yaml#/definitions/Gotham-City" |
| 31 | + }, |
| 32 | + "thing": { |
| 33 | + // references a URL |
| 34 | + "$ref": "http://wayne-enterprises.com/things/batmobile" |
| 35 | + }, |
| 36 | + "color": { |
| 37 | + // references a value in an external file via an internal reference |
| 38 | + "$ref": "#/definitions/thing/properties/colors/black-as-the-night" |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## The Solution: |
| 45 | + |
| 46 | +JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) |
| 47 | +and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most |
| 48 | +complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward |
| 49 | +JavaScript objects. |
| 50 | + |
| 51 | +- Use **JSON** or **YAML** schemas — or even a mix of both! |
| 52 | +- Supports `$ref` pointers to external files and URLs, as well as custom sources such as databases |
| 53 | +- Can bundle multiple files into a single schema that only has _internal_ `$ref` pointers |
| 54 | +- Can dereference your schema, producing a plain-old JavaScript object that's easy to work with |
| 55 | +- Supports circular references, nested references, |
| 56 | + back-references, and cross-references between files |
| 57 | +- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object |
| 58 | + instance |
| 59 | +- Compatible with Node LTS and beyond, and all major web browsers on Windows, Mac, and Linux |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +```javascript |
| 64 | +import { $RefParser } from '@hey-api/json-schema-ref-parser'; |
| 65 | + |
| 66 | +try { |
| 67 | + const parser = new $RefParser(); |
| 68 | + await parser.dereference({ pathOrUrlOrSchema: mySchema }); |
| 69 | + console.log(parser.schema.definitions.person.properties.firstName); |
| 70 | +} catch (err) { |
| 71 | + console.error(err); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### New in this fork (@hey-api) |
| 76 | + |
| 77 | +- **Multiple inputs with `bundleMany`**: Merge and bundle several OpenAPI/JSON Schema inputs (files, URLs, or raw objects) into a single schema. Components are prefixed to avoid name collisions, paths are namespaced on conflict, and `$ref`s are rewritten accordingly. |
| 78 | + |
| 79 | +```javascript |
| 80 | +import { $RefParser } from '@hey-api/json-schema-ref-parser'; |
| 81 | + |
| 82 | +const parser = new $RefParser(); |
| 83 | +const merged = await parser.bundleMany({ |
| 84 | + pathOrUrlOrSchemas: [ |
| 85 | + './specs/a.yaml', |
| 86 | + 'https://example.com/b.yaml', |
| 87 | + { openapi: '3.1.0', info: { title: 'Inline' }, paths: {} }, |
| 88 | + ], |
| 89 | +}); |
| 90 | + |
| 91 | +// merged.components.* will contain prefixed names like a_<name>, b_<name>, etc. |
| 92 | +``` |
| 93 | + |
| 94 | +- **Dereference hooks**: Fine-tune dereferencing with `excludedPathMatcher(path) => boolean` to skip subpaths and `onDereference(path, value, parent, parentPropName)` to observe replacements. |
| 95 | + |
| 96 | +```javascript |
| 97 | +const parser = new $RefParser(); |
| 98 | +parser.options.dereference.excludedPathMatcher = (p) => p.includes('/example/'); |
| 99 | +parser.options.dereference.onDereference = (p, v) => { |
| 100 | + // inspect p / v as needed |
| 101 | +}; |
| 102 | +await parser.dereference({ pathOrUrlOrSchema: './openapi.yaml' }); |
| 103 | +``` |
| 104 | + |
| 105 | +- **Smart input resolution**: You can pass a file path, URL, or raw schema object. If a raw schema includes `$id`, it is used as the base URL for resolving relative `$ref`s. |
| 106 | + |
| 107 | +```javascript |
| 108 | +await new $RefParser().bundle({ |
| 109 | + pathOrUrlOrSchema: { |
| 110 | + $id: 'https://api.example.com/openapi.json', |
| 111 | + openapi: '3.1.0', |
| 112 | + paths: { |
| 113 | + '/ping': { get: { responses: { 200: { description: 'ok' } } } }, |
| 114 | + }, |
| 115 | + }, |
| 116 | +}); |
| 117 | +``` |
0 commit comments