Skip to content

Fix #60, root references requiring a hash #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,13 @@ function refFinder (ref, schema, externalSchema) {
if (ref[0]) {
schema = externalSchema[ref[0]]
}
var walk = ref[1].split('/')
var code = 'return schema'
for (var i = 1; i < walk.length; i++) {
code += `['${walk[i]}']`
// If it has a path
if (ref[1]) {
var walk = ref[1].split('/')
for (var i = 1; i < walk.length; i++) {
code += `['${walk[i]}']`
}
}
return (new Function('schema', code))(schema)
}
Expand Down
15 changes: 13 additions & 2 deletions test/ref.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ test('ref external - properties', (t) => {
}
}
}
},
third: {
type: 'string'
}
}

Expand All @@ -109,6 +112,12 @@ test('ref external - properties', (t) => {
},
num: {
$ref: 'second#/definitions/num'
},
strPlain: {
$ref: 'third'
},
strHash: {
$ref: 'third#'
}
}
}
Expand All @@ -119,7 +128,9 @@ test('ref external - properties', (t) => {
},
num: {
int: 42
}
},
strPlain: 'test',
strHash: 'test'
}

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

t.equal(output, '{"obj":{"str":"test"},"num":{"int":42}}')
t.equal(output, '{"obj":{"str":"test"},"num":{"int":42},"strPlain":"test","strHash":"test"}')
})

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