Skip to content

Commit

Permalink
Merge pull request #87 from joaosa/master
Browse files Browse the repository at this point in the history
Detect errors on missing schema refs
  • Loading branch information
honzajavorek committed Mar 8, 2017
2 parents ab8fa38 + ee7d7aa commit 66782a7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/validators/json-schema.coffee
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
@@ -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
@@ -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
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

0 comments on commit 66782a7

Please sign in to comment.