Skip to content

Commit 21c7668

Browse files
authored
Merge pull request #61 from dougrich/master
Fix #60, root references requiring a hash
2 parents 72d870c + 2920866 commit 21c7668

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,13 @@ function refFinder (ref, schema, externalSchema) {
351351
if (ref[0]) {
352352
schema = externalSchema[ref[0]]
353353
}
354-
var walk = ref[1].split('/')
355354
var code = 'return schema'
356-
for (var i = 1; i < walk.length; i++) {
357-
code += `['${walk[i]}']`
355+
// If it has a path
356+
if (ref[1]) {
357+
var walk = ref[1].split('/')
358+
for (var i = 1; i < walk.length; i++) {
359+
code += `['${walk[i]}']`
360+
}
358361
}
359362
return (new Function('schema', code))(schema)
360363
}

test/ref.test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ test('ref external - properties', (t) => {
9797
}
9898
}
9999
}
100+
},
101+
third: {
102+
type: 'string'
100103
}
101104
}
102105

@@ -109,6 +112,12 @@ test('ref external - properties', (t) => {
109112
},
110113
num: {
111114
$ref: 'second#/definitions/num'
115+
},
116+
strPlain: {
117+
$ref: 'third'
118+
},
119+
strHash: {
120+
$ref: 'third#'
112121
}
113122
}
114123
}
@@ -119,7 +128,9 @@ test('ref external - properties', (t) => {
119128
},
120129
num: {
121130
int: 42
122-
}
131+
},
132+
strPlain: 'test',
133+
strHash: 'test'
123134
}
124135

125136
const stringify = build(schema, { schema: externalSchema })
@@ -132,7 +143,7 @@ test('ref external - properties', (t) => {
132143
t.fail()
133144
}
134145

135-
t.equal(output, '{"obj":{"str":"test"},"num":{"int":42}}')
146+
t.equal(output, '{"obj":{"str":"test"},"num":{"int":42},"strPlain":"test","strHash":"test"}')
136147
})
137148

138149
test('ref internal - patternProperties', (t) => {

0 commit comments

Comments
 (0)