diff --git a/lib/compile/index.ts b/lib/compile/index.ts index 5ff1f0cbc0..3dac2699b2 100644 --- a/lib/compile/index.ts +++ b/lib/compile/index.ts @@ -208,7 +208,7 @@ export function resolveRef( baseId: string, ref: string ): AnySchema | SchemaEnv | undefined { - ref = resolveUrl(baseId, ref, this.opts.uriResolver) + ref = resolveUrl(this.opts.uriResolver, baseId, ref) const schOrFunc = root.refs[ref] if (schOrFunc) return schOrFunc @@ -258,7 +258,7 @@ export function resolveSchema( ref: string // reference to resolve ): SchemaEnv | undefined { const p = this.opts.uriResolver.parse(ref) - const refPath = _getFullPath(p, this.opts.uriResolver) + const refPath = _getFullPath(this.opts.uriResolver, p) let baseId = getFullPath(this.opts.uriResolver, root.baseId, undefined) // TODO `Object.keys(root.schema).length > 0` should not be needed - but removing breaks 2 tests if (Object.keys(root.schema).length > 0 && refPath === baseId) { @@ -279,7 +279,7 @@ export function resolveSchema( const {schema} = schOrRef const {schemaId} = this.opts const schId = schema[schemaId] - if (schId) baseId = resolveUrl(baseId, schId, this.opts.uriResolver) + if (schId) baseId = resolveUrl(this.opts.uriResolver, baseId, schId) return new SchemaEnv({schema, schemaId, root, baseId}) } return getJsonPointer.call(this, p, schOrRef) @@ -307,12 +307,12 @@ function getJsonPointer( // TODO PREVENT_SCOPE_CHANGE could be defined in keyword def? const schId = typeof schema === "object" && schema[this.opts.schemaId] if (!PREVENT_SCOPE_CHANGE.has(part) && schId) { - baseId = resolveUrl(baseId, schId, this.opts.uriResolver) + baseId = resolveUrl(this.opts.uriResolver, baseId, schId) } } let env: SchemaEnv | undefined if (typeof schema != "boolean" && schema.$ref && !schemaHasRulesButRef(schema, this.RULES)) { - const $ref = resolveUrl(baseId, schema.$ref, this.opts.uriResolver) + const $ref = resolveUrl(this.opts.uriResolver, baseId, schema.$ref) env = resolveSchema.call(this, root, $ref) } // even though resolution failed we need to return SchemaEnv to throw exception diff --git a/lib/compile/jtd/serialize.ts b/lib/compile/jtd/serialize.ts index 7130b0088b..7ebd26acbb 100644 --- a/lib/compile/jtd/serialize.ts +++ b/lib/compile/jtd/serialize.ts @@ -234,7 +234,7 @@ function serializeRef(cxt: SerializeCxt): void { const {gen, self, data, definitions, schema, schemaEnv} = cxt const {ref} = schema const refSchema = definitions[ref] - if (!refSchema) throw new MissingRefError("", ref, self.opts.uriResolver, `No definition ${ref}`) + if (!refSchema) throw new MissingRefError(self.opts.uriResolver, "", ref, `No definition ${ref}`) if (!hasRef(refSchema)) return serializeCode({...cxt, schema: refSchema}) const {root} = schemaEnv const sch = compileSerializer.call(self, new SchemaEnv({schema: refSchema, root}), definitions) diff --git a/lib/compile/ref_error.ts b/lib/compile/ref_error.ts index 2fda171fff..386bf04995 100644 --- a/lib/compile/ref_error.ts +++ b/lib/compile/ref_error.ts @@ -5,9 +5,9 @@ export default class MissingRefError extends Error { readonly missingRef: string readonly missingSchema: string - constructor(baseId: string, ref: string, resolver: UriResolver, msg?: string) { + constructor(resolver: UriResolver, baseId: string, ref: string, msg?: string) { super(msg || `can't resolve reference ${ref} from id ${baseId}`) - this.missingRef = resolveUrl(baseId, ref, resolver) + this.missingRef = resolveUrl(resolver, baseId, ref) this.missingSchema = normalizeId(getFullPath(resolver, this.missingRef)) } } diff --git a/lib/compile/resolve.ts b/lib/compile/resolve.ts index 038cea1c33..4360eab068 100644 --- a/lib/compile/resolve.ts +++ b/lib/compile/resolve.ts @@ -70,10 +70,10 @@ function countKeys(schema: AnySchemaObject): number { export function getFullPath(resolver: UriResolver, id = "", normalize?: boolean): string { if (normalize !== false) id = normalizeId(id) const p = resolver.parse(id) - return _getFullPath(p, resolver) + return _getFullPath(resolver, p) } -export function _getFullPath(p: URIComponents, resolver: UriResolver): string { +export function _getFullPath(resolver: UriResolver, p: URIComponents): string { const serialized = resolver.serialize(p) return serialized.split("#")[0] + "#" } @@ -83,7 +83,7 @@ export function normalizeId(id: string | undefined): string { return id ? id.replace(TRAILING_SLASH_HASH, "") : "" } -export function resolveUrl(baseId: string, id: string, resolver: UriResolver): string { +export function resolveUrl(resolver: UriResolver, baseId: string, id: string): string { id = normalizeId(id) return resolver.resolve(baseId, id) } diff --git a/lib/vocabularies/jtd/ref.ts b/lib/vocabularies/jtd/ref.ts index 5f57b6d655..97646ee1b6 100644 --- a/lib/vocabularies/jtd/ref.ts +++ b/lib/vocabularies/jtd/ref.ts @@ -29,7 +29,7 @@ const def: CodeKeywordDefinition = { function validateJtdRef(): void { const refSchema = (root.schema as AnySchemaObject).definitions?.[ref] if (!refSchema) { - throw new MissingRefError("", ref, it.opts.uriResolver, `No definition ${ref}`) + throw new MissingRefError(it.opts.uriResolver, "", ref, `No definition ${ref}`) } if (hasRef(refSchema) || !it.opts.inlineRefs) callValidate(refSchema) else inlineRefSchema(refSchema) diff --git a/spec/keyword.spec.ts b/spec/keyword.spec.ts index 81c8a79cae..e8930b0d59 100644 --- a/spec/keyword.spec.ts +++ b/spec/keyword.spec.ts @@ -282,7 +282,7 @@ describe("User-defined keywords", () => { it.baseId.should.equal("#") const ref = schema.$ref const validate = _ajv.getSchema(ref) - if (!validate) throw new _Ajv.MissingRefError(it.baseId, ref, _ajv.opts.uriResolver) + if (!validate) throw new _Ajv.MissingRefError(_ajv.opts.uriResolver, it.baseId, ref) return validate.schema }, metaSchema: {