Skip to content

Commit

Permalink
fix: order
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth committed Jan 20, 2022
1 parent 227c060 commit bcfc31d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/compile/index.ts
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/compile/jtd/serialize.ts
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/compile/ref_error.ts
Expand Up @@ -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))
}
}
6 changes: 3 additions & 3 deletions lib/compile/resolve.ts
Expand Up @@ -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] + "#"
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vocabularies/jtd/ref.ts
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/keyword.spec.ts
Expand Up @@ -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: {
Expand Down

0 comments on commit bcfc31d

Please sign in to comment.