Skip to content

Commit 0f8f7e0

Browse files
authored
Add check for $ref needing AJV (#316)
1 parent 3054a86 commit 0f8f7e0

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,19 @@ function inferTypeByKeyword (schema) {
225225

226226
function dependsOnAjv (schema) {
227227
const str = JSON.stringify(schema)
228-
return (/"if":{.*"then":{|"(anyOf|oneOf)":\[|"const":/.test(str))
228+
switch (true) {
229+
case /"if":{.*"then":{/.test(str):
230+
case /"(anyOf|oneOf)":\[/.test(str):
231+
case /"const"/.test(str):
232+
case /"\$ref"/.test(str):
233+
{
234+
return true
235+
}
236+
237+
default: {
238+
return false
239+
}
240+
}
229241
}
230242

231243
const stringSerializerMap = {

test/requiresAjv.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const build = require('..')
5+
6+
t.test('nested ref requires ajv', async t => {
7+
const schemaA = {
8+
$id: 'urn:schema:a',
9+
definitions: {
10+
foo: { anyOf: [{ type: 'string' }, { type: 'null' }] }
11+
}
12+
}
13+
14+
const schemaB = {
15+
$id: 'urn:schema:b',
16+
type: 'object',
17+
properties: {
18+
results: {
19+
type: 'object',
20+
properties: {
21+
items: {
22+
type: 'object',
23+
properties: {
24+
bar: {
25+
type: 'array',
26+
items: { $ref: 'urn:schema:a#/definitions/foo' }
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
const stringify = build(schemaB, {
36+
schema: {
37+
[schemaA.$id]: schemaA
38+
}
39+
})
40+
const result = stringify({
41+
results: {
42+
items: {
43+
bar: ['baz']
44+
}
45+
}
46+
})
47+
t.same(result, '{"results":{"items":{"bar":["baz"]}}}')
48+
})

0 commit comments

Comments
 (0)