Skip to content

Commit

Permalink
test: add test cases regarding JSON schema $ref validation
Browse files Browse the repository at this point in the history
Add a positive test case and two negative test cases (valid data with invalid schema ref and invalid data with valid schema ref)
  • Loading branch information
joaosa committed Feb 28, 2017
1 parent 20669aa commit 13d916d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
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

0 comments on commit 13d916d

Please sign in to comment.