Skip to content

Commit 61bf10e

Browse files
committed
Fix bug where ajv eagerly resolves ref in array items
1 parent f20bd60 commit 61bf10e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/schemas.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const Dereference = {
2222

2323
if (Array.isArray(value)) {
2424
// Schema definition returns an array for this property, return the array with all refs resolved
25-
return value.map((item, i) => Schema.proxy(this.join(target.$id, `${property}/${i}`), item))
25+
return value.map((item, i) => {
26+
const $ref = item.$ref || this.join(target.$id, `${property}/${i}`)
27+
return Schema.resolve($ref, target.$id)
28+
})
2629
} else if (value !== null && typeof value === 'object') {
2730
// Schema definition returns an object for this property, return the subschema and proxy it
2831
return Schema.proxy(this.join(target.$id, property), value)
@@ -54,10 +57,15 @@ const DelegateToDefinition = {
5457
}
5558

5659
// Class to browse schemas, resolve refs, and validate data
57-
class Schema {
58-
static resolve ($ref, $id = undefined) {
60+
export class Schema {
61+
static resolve ($ref, $id = BaseURI) {
5962
const { href } = new URL($ref, $id)
60-
return this.proxy(href, ajv.getSchema(href).schema)
63+
const validator = ajv.getSchema(href)
64+
65+
if (!validator) throw new TypeError('Schema not found: ' + href)
66+
if (!validator.proxy) validator.proxy = Schema.proxy(href, validator.schema)
67+
68+
return validator.proxy
6169
}
6270

6371
static proxy ($id, definition) {
@@ -89,4 +97,4 @@ class Schema {
8997
}
9098
}
9199

92-
export const schema = Schema.resolve(BaseURI)
100+
export const schema = Schema.resolve('#')

0 commit comments

Comments
 (0)