Skip to content
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

Detect errors on missing schema refs #87

Merged
merged 2 commits into from
Mar 8, 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
15 changes: 13 additions & 2 deletions src/validators/json-schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,23 @@ class JsonSchema
#@private
validateSchemaV4: =>
result = tv4.validateMultiple @data, @schema
validationErrors = result.errors.concat result.missing

amandaCompatibleError =
length: result.errors.length
length: validationErrors.length
errorMessages: {}

for error, index in result?.errors
for err, index in validationErrors
# Need to create the error in case of missing schema errors.
# If tv4 meets $ref to a missing schema, it provides an array that contains the schema's domain.
# In case we a local $ref, as there's no domain name, it will look like this: ['', '': '']
if err instanceof Error
error = err
else
error = new Error('Missing schema')
error.params = { key: err }
error.dataPath = ''

pathArray = jsonPointer.parse error.dataPath
if error.params.key
pathArray.push error.params.key
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/invalid-schema-v4-with-refs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"bar": {
"type": "string"
}
},
"type": "object",
"required": ["foo"],
"additionalProperties": false,
"properties": {
"foo": {
"$ref": "#/definitions/baz"
}
}
}
16 changes: 16 additions & 0 deletions test/fixtures/valid-schema-v4-with-refs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"bar": {
"type": "string"
}
},
"type": "object",
"required": ["foo"],
"additionalProperties": false,
"properties": {
"foo": {
"$ref": "#/definitions/bar"
}
}
}
57 changes: 56 additions & 1 deletion test/unit/validators/json-schema-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ describe 'validatePrivate()', () ->
it 'should not valiate using tv4', () ->
assert.notOk validator.validateSchemaV4.called


describe 'when @jsonSchemaVersion is set to v4', () ->
validator = null

Expand All @@ -307,6 +306,62 @@ describe 'validatePrivate()', () ->
it 'should not validate using amanda', () ->
assert.notOk validator.validateSchemaV3.called

describe 'when valid v4 $ref schema provided with valid data', () ->
fn = null
validator = null

before () ->
validSchema = require '../../fixtures/valid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": "bar" }
'''
fn = () ->
validator = new JsonSchema real, validSchema
validator.validatePrivate()

it 'should not return a validation error', () ->
assert.lengthOf fn(), 0

describe 'when valid v4 $ref schema provided with invalid data', () ->
fn = null
validator = null

before () ->
validSchema = require '../../fixtures/valid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": 1 }
'''
fn = () ->
validator = new JsonSchema real, validSchema
validator.validatePrivate()

it 'should return a validation error', () ->
assert.lengthOf fn(), 1

it 'should mention an invalid type error', () ->
e = fn()[0]
assert.include e.message, "Invalid type"

describe 'when invalid v4 $ref schema provided with valid data', () ->
fn = null
validator = null

before () ->
invalidSchema = require '../../fixtures/invalid-schema-v4-with-refs'
real = JSON.parse '''
{ "foo": "bar" }
'''
fn = () ->
validator = new JsonSchema real, invalidSchema
validator.validatePrivate()

it 'should return a validation error', () ->
assert.lengthOf fn(), 1

it 'should mention a missing schema error', () ->
e = fn()[0]
assert.include e.message, "Missing schema"

describe 'when @jsonSchemaVersion is null', () ->
fn = null
validator = null
Expand Down