json-schema-ref-resolver
Advanced tools
| name: CI | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - master | ||
| - next | ||
| - 'v*' | ||
| paths-ignore: | ||
| - 'docs/**' | ||
| - '*.md' | ||
| pull_request: | ||
| paths-ignore: | ||
| - 'docs/**' | ||
| - '*.md' | ||
| jobs: | ||
| test: | ||
| uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.2.0 | ||
| with: | ||
| license-check: true | ||
| lint: true | ||
| node-versions: '["18", "20"]' |
+52
-39
| 'use strict' | ||
| const deepEqual = require('fast-deep-equal') | ||
| const { dequal: deepEqual } = require('dequal') | ||
@@ -22,11 +22,37 @@ const jsonSchemaRefSymbol = Symbol.for('json-schema-ref') | ||
| addSchema (schema, schemaId) { | ||
| if (schema.$id !== undefined && schema.$id.charAt(0) !== '#') { | ||
| // Schema has an $id that is not an anchor | ||
| schemaId = schema.$id | ||
| } else { | ||
| // Schema has no $id or $id is an anchor | ||
| this.#insertSchemaBySchemaId(schema, schemaId) | ||
| addSchema (schema, rootSchemaId, isRootSchema = true) { | ||
| if (isRootSchema) { | ||
| if (schema.$id !== undefined && schema.$id.charAt(0) !== '#') { | ||
| // Schema has an $id that is not an anchor | ||
| rootSchemaId = schema.$id | ||
| } else { | ||
| // Schema has no $id or $id is an anchor | ||
| this.#insertSchemaBySchemaId(schema, rootSchemaId) | ||
| } | ||
| } | ||
| this.#addSchema(schema, schemaId) | ||
| const schemaId = schema.$id | ||
| if (schemaId !== undefined && typeof schemaId === 'string') { | ||
| if (schemaId.charAt(0) === '#') { | ||
| this.#insertSchemaByAnchor(schema, rootSchemaId, schemaId) | ||
| } else { | ||
| this.#insertSchemaBySchemaId(schema, schemaId) | ||
| rootSchemaId = schemaId | ||
| } | ||
| } | ||
| const ref = schema.$ref | ||
| if (ref !== undefined && typeof ref === 'string') { | ||
| const { refSchemaId, refJsonPointer } = this.#parseSchemaRef(ref, rootSchemaId) | ||
| this.#schemas[rootSchemaId].refs.push({ | ||
| schemaId: refSchemaId, | ||
| jsonPointer: refJsonPointer | ||
| }) | ||
| } | ||
| for (const key in schema) { | ||
| if (typeof schema[key] === 'object' && schema[key] !== null) { | ||
| this.addSchema(schema[key], rootSchemaId, false) | ||
| } | ||
| } | ||
| } | ||
@@ -64,3 +90,7 @@ | ||
| const dependencySchemaId = ref.schemaId | ||
| if (dependencies[dependencySchemaId] !== undefined) continue | ||
| if ( | ||
| dependencySchemaId === schemaId || | ||
| dependencies[dependencySchemaId] !== undefined | ||
| ) continue | ||
| dependencies[dependencySchemaId] = this.getSchema(dependencySchemaId) | ||
@@ -89,3 +119,3 @@ this.getSchemaDependencies(dependencySchemaId, dependencies) | ||
| const refs = [] | ||
| this.#addDerefSchema(schema.schema, schemaId, refs) | ||
| this.#addDerefSchema(schema.schema, schemaId, true, refs) | ||
@@ -95,3 +125,3 @@ const dependencies = this.getSchemaDependencies(schemaId) | ||
| const schema = dependencies[schemaId] | ||
| this.#addDerefSchema(schema, schemaId, refs) | ||
| this.#addDerefSchema(schema, schemaId, true, refs) | ||
| } | ||
@@ -147,32 +177,15 @@ | ||
| #addSchema (schema, rootSchemaId) { | ||
| const schemaId = schema.$id | ||
| if (schemaId !== undefined && typeof schemaId === 'string') { | ||
| if (schemaId.charAt(0) === '#') { | ||
| this.#insertSchemaByAnchor(schema, rootSchemaId, schemaId) | ||
| #addDerefSchema (schema, rootSchemaId, isRootSchema, refs = []) { | ||
| const derefSchema = Array.isArray(schema) ? [...schema] : { ...schema } | ||
| if (isRootSchema) { | ||
| if (schema.$id !== undefined && schema.$id.charAt(0) !== '#') { | ||
| // Schema has an $id that is not an anchor | ||
| rootSchemaId = schema.$id | ||
| } else { | ||
| this.#insertSchemaBySchemaId(schema, schemaId) | ||
| rootSchemaId = schemaId | ||
| // Schema has no $id or $id is an anchor | ||
| this.#insertDerefSchemaBySchemaId(derefSchema, rootSchemaId) | ||
| } | ||
| } | ||
| const ref = schema.$ref | ||
| if (ref !== undefined && typeof ref === 'string') { | ||
| const { refSchemaId, refJsonPointer } = this.#parseSchemaRef(ref, rootSchemaId) | ||
| this.#schemas[rootSchemaId].refs.push({ | ||
| schemaId: refSchemaId, | ||
| jsonPointer: refJsonPointer | ||
| }) | ||
| } | ||
| for (const key in schema) { | ||
| if (typeof schema[key] === 'object' && schema[key] !== null) { | ||
| this.#addSchema(schema[key], rootSchemaId) | ||
| } | ||
| } | ||
| } | ||
| #addDerefSchema (schema, rootSchemaId, refs = []) { | ||
| const derefSchema = Array.isArray(schema) ? [...schema] : { ...schema } | ||
| const schemaId = derefSchema.$id | ||
@@ -199,3 +212,3 @@ if (schemaId !== undefined && typeof schemaId === 'string') { | ||
| if (typeof value === 'object' && value !== null) { | ||
| derefSchema[key] = this.#addDerefSchema(value, rootSchemaId, refs) | ||
| derefSchema[key] = this.#addDerefSchema(value, rootSchemaId, false, refs) | ||
| } | ||
@@ -202,0 +215,0 @@ } |
+7
-4
| { | ||
| "name": "json-schema-ref-resolver", | ||
| "version": "1.0.1", | ||
| "version": "2.0.0", | ||
| "description": "JSON schema reference resolver", | ||
| "main": "index.js", | ||
| "type": "commonjs", | ||
| "types": "types/index.d.ts", | ||
@@ -31,9 +32,11 @@ "scripts": { | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/fastify/json-schema-ref-resolver#readme", | ||
| "dependencies": { | ||
| "fast-deep-equal": "^3.1.3" | ||
| "dequal": "^2.0.3" | ||
| }, | ||
| "devDependencies": { | ||
| "c8": "^8.0.1", | ||
| "@fastify/pre-commit": "^2.1.0", | ||
| "c8": "^9.1.0", | ||
| "standard": "^17.1.0", | ||
| "tsd": "^0.29.0" | ||
| "tsd": "^0.31.0" | ||
| }, | ||
@@ -40,0 +43,0 @@ "standard": { |
@@ -237,1 +237,52 @@ 'use strict' | ||
| }) | ||
| test('should deref schema without root $id', () => { | ||
| const refResolver = new RefResolver() | ||
| const schemaId = 'schemaId1' | ||
| const schema = { | ||
| type: 'object', | ||
| definitions: { | ||
| id1: { | ||
| type: 'object', | ||
| properties: { | ||
| id1: { | ||
| type: 'integer' | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| allOf: [ | ||
| { | ||
| $ref: '#/definitions/id1' | ||
| } | ||
| ] | ||
| } | ||
| refResolver.addSchema(schema, schemaId) | ||
| const derefSchema = refResolver.getDerefSchema(schemaId) | ||
| assert.deepStrictEqual(derefSchema, { | ||
| type: 'object', | ||
| definitions: { | ||
| id1: { | ||
| type: 'object', | ||
| properties: { | ||
| id1: { | ||
| type: 'integer' | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| allOf: [ | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| id1: { | ||
| type: 'integer' | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| }) |
| name: Run Tests | ||
| on: [push, pull_request] | ||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| test: | ||
| name: Test | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| matrix: | ||
| node-version: [18.x, 20.x] | ||
| os: [ubuntu-latest, windows-latest, macOS-latest] | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v3 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - name: Install | ||
| run: | | ||
| npm install | ||
| - name: Test | ||
| run: | | ||
| npm run test |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
57210
2.24%1492
4.04%0
-100%4
33.33%1
Infinity%+ Added
+ Added
- Removed
- Removed