Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: use json-schema-ref-resolver #653

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
const merge = require('@fastify/deepmerge')()
const clone = require('rfdc')({ proto: true })
const { randomUUID } = require('node:crypto')
const { RefResolver } = require('json-schema-ref-resolver')

const validate = require('./lib/schema-validator')
const Serializer = require('./lib/serializer')
const Validator = require('./lib/validator')
const RefResolver = require('./lib/ref-resolver')
const Location = require('./lib/location')

let largeArraySize = 2e4
Expand Down Expand Up @@ -53,8 +53,7 @@ function resolveRef (context, location, ref) {
const jsonPointer = ref.slice(hashIndex) || '#'

const schema = context.refResolver.getSchema(schemaId, jsonPointer)

if (schema === undefined) {
if (schema === null) {
throw new Error(`Cannot find reference "${ref}"`)
}

Expand All @@ -66,6 +65,13 @@ function resolveRef (context, location, ref) {
return newLocation
}

function getSchemaId (schema, rootSchemaId) {
if (schema.$id && schema.$id.charAt(0) !== '#') {
return schema.$id
}
return rootSchemaId
}

function build (schema, options) {
isValidSchema(schema)

Expand All @@ -82,12 +88,19 @@ function build (schema, options) {
validatorSchemasIds: new Set()
}

context.refResolver.addSchema(schema, context.rootSchemaId)
const schemaId = getSchemaId(schema, context.rootSchemaId)
if (!context.refResolver.hasSchema(schemaId)) {
context.refResolver.addSchema(schema, context.rootSchemaId)
}

if (options.schema) {
for (const key of Object.keys(options.schema)) {
isValidSchema(options.schema[key], key)
context.refResolver.addSchema(options.schema[key], key)
for (const key in options.schema) {
const schema = options.schema[key]
const schemaId = getSchemaId(schema, key)
if (!context.refResolver.hasSchema(schemaId)) {
isValidSchema(schema, key)
context.refResolver.addSchema(schema, key)
}
}
}

Expand Down
104 changes: 0 additions & 104 deletions lib/ref-resolver.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"ajv-formats": "^2.1.1",
"fast-deep-equal": "^3.1.3",
"fast-uri": "^2.1.0",
"rfdc": "^1.2.0"
"rfdc": "^1.2.0",
"json-schema-ref-resolver": "^1.0.1"
},
"standard": {
"ignore": [
Expand Down
17 changes: 14 additions & 3 deletions test/ref.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,10 @@ test('Bad key', t => {
})
t.fail('Should throw')
} catch (err) {
t.equal(err.message, 'Cannot find reference "extrenal#/definitions/projectId"')
t.equal(
err.message,
'Cannot resolve ref "extrenal#/definitions/projectId". Schema with id "extrenal" is not found.'
)
}
})

Expand Down Expand Up @@ -2076,7 +2079,11 @@ test('should throw an Error if two non-identical schemas with same id are provid
]
}

t.throws(() => build(schema), new Error('There is already another schema with id inner_schema'))
try {
build(schema)
} catch (err) {
t.equal(err.message, 'There is already another schema with id "inner_schema".')
}
})

test('ref internal - throw if schema has definition twice with different shape', (t) => {
Expand Down Expand Up @@ -2115,5 +2122,9 @@ test('ref internal - throw if schema has definition twice with different shape',
}
}

t.throws(() => build(schema), Error('There is already another schema with id test##uri'))
try {
build(schema)
} catch (err) {
t.equal(err.message, 'There is already another anchor "#uri" in a schema "test".')
}
})
Loading