@apidevtools/json-schema-ref-parser
Advanced tools
Comparing version 11.2.4 to 11.3.0
@@ -132,3 +132,2 @@ "use strict"; | ||
function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) { | ||
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path); | ||
const isExternalRef = ref_js_1.default.isExternal$Ref($ref); | ||
@@ -135,0 +134,0 @@ const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root"; |
@@ -21,2 +21,6 @@ "use strict"; | ||
/** | ||
* Allow JSON files with byte order marks (BOM) | ||
*/ | ||
allowBOM: true, | ||
/** | ||
* Parses the given file as JSON | ||
@@ -38,2 +42,14 @@ */ | ||
catch (e) { | ||
if (this.allowBOM) { | ||
try { | ||
// find the first curly brace | ||
const firstCurlyBrace = data.indexOf("{"); | ||
// remove any characters before the first curly brace | ||
data = data.slice(firstCurlyBrace); | ||
return JSON.parse(data); | ||
} | ||
catch (e) { | ||
throw new errors_js_1.ParserError(e.message, file.url); | ||
} | ||
} | ||
throw new errors_js_1.ParserError(e.message, file.url); | ||
@@ -40,0 +56,0 @@ } |
@@ -39,3 +39,3 @@ import type $RefParserOptions from "./options.js"; | ||
indirections: number; | ||
constructor($ref: any, path: any, friendlyPath?: string); | ||
constructor($ref: $Ref, path: string, friendlyPath?: string); | ||
/** | ||
@@ -42,0 +42,0 @@ * Resolves the value of a nested property within the given object. |
@@ -88,2 +88,16 @@ "use strict"; | ||
if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) { | ||
// one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again | ||
let didFindSubstringSlashMatch = false; | ||
for (let j = tokens.length - 1; j > i; j--) { | ||
const joinedToken = tokens.slice(i, j + 1).join("/"); | ||
if (this.value[joinedToken] !== undefined) { | ||
this.value = this.value[joinedToken]; | ||
i = j; | ||
didFindSubstringSlashMatch = true; | ||
break; | ||
} | ||
} | ||
if (didFindSubstringSlashMatch) { | ||
continue; | ||
} | ||
this.value = null; | ||
@@ -90,0 +104,0 @@ throw new errors_js_1.MissingPointerError(token, decodeURI(this.originalPath)); |
@@ -68,2 +68,8 @@ /// <reference types="node" /> | ||
/** | ||
* Specifies whether a Byte Order Mark (BOM) is allowed or not. Only applies to JSON parsing | ||
* | ||
* @type {boolean} @default true | ||
*/ | ||
allowBOM?: boolean; | ||
/** | ||
* The encoding that the text is expected to be in. | ||
@@ -70,0 +76,0 @@ */ |
@@ -162,12 +162,10 @@ import $Ref from "./ref.js"; | ||
$ref: any, | ||
path: any, | ||
pathFromRoot: any, | ||
parents: any, | ||
path: string, | ||
pathFromRoot: string, | ||
parents: Set<any>, | ||
processedObjects: any, | ||
dereferencedCache: any, | ||
$refs: any, | ||
options: any, | ||
$refs: $Refs, | ||
options: $RefParserOptions, | ||
) { | ||
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path); | ||
const isExternalRef = $Ref.isExternal$Ref($ref); | ||
@@ -174,0 +172,0 @@ const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root"; |
@@ -25,2 +25,7 @@ import { ParserError } from "../util/errors.js"; | ||
/** | ||
* Allow JSON files with byte order marks (BOM) | ||
*/ | ||
allowBOM: true, | ||
/** | ||
* Parses the given file as JSON | ||
@@ -41,2 +46,13 @@ */ | ||
} catch (e: any) { | ||
if (this.allowBOM) { | ||
try { | ||
// find the first curly brace | ||
const firstCurlyBrace = data.indexOf("{"); | ||
// remove any characters before the first curly brace | ||
data = data.slice(firstCurlyBrace); | ||
return JSON.parse(data); | ||
} catch (e: any) { | ||
throw new ParserError(e.message, file.url); | ||
} | ||
} | ||
throw new ParserError(e.message, file.url); | ||
@@ -43,0 +59,0 @@ } |
@@ -6,2 +6,3 @@ import type $RefParserOptions from "./options.js"; | ||
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js"; | ||
const slashes = /\//g; | ||
@@ -61,3 +62,3 @@ const tildes = /~/g; | ||
constructor($ref: any, path: any, friendlyPath?: string) { | ||
constructor($ref: $Ref, path: string, friendlyPath?: string) { | ||
this.$ref = $ref; | ||
@@ -107,2 +108,17 @@ | ||
if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) { | ||
// one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again | ||
let didFindSubstringSlashMatch = false; | ||
for (let j = tokens.length - 1; j > i; j--) { | ||
const joinedToken = tokens.slice(i, j + 1).join("/"); | ||
if (this.value[joinedToken] !== undefined) { | ||
this.value = this.value[joinedToken]; | ||
i = j; | ||
didFindSubstringSlashMatch = true; | ||
break; | ||
} | ||
} | ||
if (didFindSubstringSlashMatch) { | ||
continue; | ||
} | ||
this.value = null; | ||
@@ -109,0 +125,0 @@ throw new MissingPointerError(token, decodeURI(this.originalPath)); |
@@ -92,2 +92,9 @@ import type { | ||
/** | ||
* Specifies whether a Byte Order Mark (BOM) is allowed or not. Only applies to JSON parsing | ||
* | ||
* @type {boolean} @default true | ||
*/ | ||
allowBOM?: boolean; | ||
/** | ||
* The encoding that the text is expected to be in. | ||
@@ -94,0 +101,0 @@ */ |
{ | ||
"name": "@apidevtools/json-schema-ref-parser", | ||
"version": "11.2.4", | ||
"version": "11.3.0", | ||
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
141
README.md
@@ -12,7 +12,19 @@ # JSON Schema $Ref Parser | ||
Installation | ||
-------------------------- | ||
Install using [npm](https://docs.npmjs.com/about-npm/): | ||
```bash | ||
npm install @apidevtools/json-schema-ref-parser | ||
yarn add @apidevtools/json-schema-ref-parser | ||
bun add @apidevtools/json-schema-ref-parser | ||
``` | ||
The Problem: | ||
-------------------------- | ||
You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead 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 format. Maybe some of the files contain cross-references to each other. | ||
You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead | ||
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 | ||
format. Maybe some of the files contain cross-references to each other. | ||
```javascript | ||
```json | ||
{ | ||
@@ -40,16 +52,22 @@ "definitions": { | ||
The Solution: | ||
-------------------------- | ||
JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward JavaScript objects. | ||
JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) | ||
and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most | ||
complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward | ||
JavaScript objects. | ||
- Use **JSON** or **YAML** schemas — or even a mix of both! | ||
- Supports `$ref` pointers to external files and URLs, as well as [custom sources](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) such as databases | ||
- Can [bundle](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) multiple files into a single schema that only has _internal_ `$ref` pointers | ||
- Can [dereference](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferencepath-options-callback) your schema, producing a plain-old JavaScript object that's easy to work with | ||
- Supports [circular references](https://apitools.dev/json-schema-ref-parser/docs/#circular-refs), nested references, back-references, and cross-references between files | ||
- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object instance | ||
- Tested in Node v10, v12, & v14, and all major web browsers on Windows, Mac, and Linux | ||
- Supports `$ref` pointers to external files and URLs, as well | ||
as [custom sources](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) such as databases | ||
- Can [bundle](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) multiple | ||
files into a single schema that only has _internal_ `$ref` pointers | ||
- Can [dereference](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferencepath-options-callback) | ||
your schema, producing a plain-old JavaScript object that's easy to work with | ||
- Supports [circular references](https://apitools.dev/json-schema-ref-parser/docs/#circular-refs), nested references, | ||
back-references, and cross-references between files | ||
- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object | ||
instance | ||
- Compatible with Node LTS and beyond, and all major web browsers on Windows, Mac, and Linux | ||
Example | ||
@@ -59,22 +77,8 @@ -------------------------- | ||
```javascript | ||
$RefParser.dereference(mySchema, (err, schema) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
else { | ||
// `schema` is just a normal JavaScript object that contains your entire JSON Schema, | ||
// including referenced files, combined into a single object | ||
console.log(schema.definitions.person.properties.firstName); | ||
} | ||
}) | ||
``` | ||
import $RefParser from "@apidevtools/json-schema-ref-parser"; | ||
Or use `async`/`await` syntax instead. The following example is the same as above: | ||
```javascript | ||
try { | ||
let schema = await $RefParser.dereference(mySchema); | ||
console.log(schema.definitions.person.properties.firstName); | ||
} | ||
catch(err) { | ||
} catch (err) { | ||
console.error(err); | ||
@@ -87,29 +91,10 @@ } | ||
Installation | ||
Polyfills | ||
-------------------------- | ||
Install using [npm](https://docs.npmjs.com/about-npm/): | ||
```bash | ||
npm install @apidevtools/json-schema-ref-parser | ||
``` | ||
If you are using Node.js < 18, you'll need a polyfill for `fetch`, | ||
like [node-fetch](https://github.com/node-fetch/node-fetch): | ||
Usage | ||
-------------------------- | ||
When using JSON Schema $Ref Parser in Node.js apps, you'll probably want to use **CommonJS** syntax: | ||
```javascript | ||
const $RefParser = require("@apidevtools/json-schema-ref-parser"); | ||
``` | ||
When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](https://www.typescriptlang.org/), or a bundler such as [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/), you can use **ECMAScript modules** syntax instead: | ||
```javascript | ||
import $RefParser from "@apidevtools/json-schema-ref-parser"; | ||
``` | ||
If you are using Node.js < 18, you'll need a polyfill for `fetch`, like [node-fetch](https://github.com/node-fetch/node-fetch): | ||
```javascript | ||
import fetch from "node-fetch"; | ||
@@ -120,34 +105,32 @@ | ||
Browser support | ||
-------------------------- | ||
JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill). | ||
JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may | ||
require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill). | ||
To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve). | ||
To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such | ||
as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), | ||
or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as | ||
setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve). | ||
#### Webpack 5 | ||
#### Webpack 5 | ||
Webpack 5 has dropped the default export of node core modules in favour of polyfills, you'll need to set them up yourself ( after npm-installing them ) | ||
Webpack 5 has dropped the default export of node core modules in favour of polyfills, you'll need to set them up | ||
yourself ( after npm-installing them ) | ||
Edit your `webpack.config.js` : | ||
```js | ||
config.resolve.fallback = { | ||
"path": require.resolve("path-browserify"), | ||
'util': require.resolve('util/'), | ||
'fs': require.resolve('browserify-fs'), | ||
"buffer": require.resolve("buffer/"), | ||
"http": require.resolve("stream-http"), | ||
"https": require.resolve("https-browserify"), | ||
"url": require.resolve("url"), | ||
} | ||
config.resolve.fallback = { | ||
"path": require.resolve("path-browserify"), | ||
'fs': require.resolve('browserify-fs') | ||
} | ||
config.plugins.push( | ||
new webpack.ProvidePlugin({ | ||
Buffer: [ 'buffer', 'Buffer'] | ||
}) | ||
) | ||
config.plugins.push( | ||
new webpack.ProvidePlugin({ | ||
Buffer: ['buffer', 'Buffer'] | ||
}) | ||
) | ||
``` | ||
API Documentation | ||
@@ -158,21 +141,21 @@ -------------------------- | ||
Contributing | ||
-------------------------- | ||
I welcome any contributions, enhancements, and bug-fixes. [Open an issue](https://github.com/APIDevTools/json-schema-ref-parser/issues) on GitHub and [submit a pull request](https://github.com/APIDevTools/json-schema-ref-parser/pulls). | ||
I welcome any contributions, enhancements, and | ||
bug-fixes. [Open an issue](https://github.com/APIDevTools/json-schema-ref-parser/issues) on GitHub | ||
and [submit a pull request](https://github.com/APIDevTools/json-schema-ref-parser/pulls). | ||
#### Building/Testing | ||
To build/test the project locally on your computer: | ||
1. __Clone this repo__<br> | ||
`git clone https://github.com/APIDevTools/json-schema-ref-parser.git` | ||
`git clone https://github.com/APIDevTools/json-schema-ref-parser.git` | ||
2. __Install dependencies__<br> | ||
`npm install` | ||
`yarn install` | ||
3. __Run the tests__<br> | ||
`npm test` | ||
`yarn test` | ||
License | ||
@@ -182,3 +165,5 @@ -------------------------- | ||
This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. | ||
This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a | ||
tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work. By contributing to | ||
the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. | ||
@@ -185,0 +170,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
299066
77
7159
172