From 13d916d146fc0ebfe2de60ae5d7aba4ccbccaa98 Mon Sep 17 00:00:00 2001 From: Joao Andrade Date: Tue, 20 Sep 2016 21:34:08 +0100 Subject: [PATCH 1/2] test: add test cases regarding JSON schema $ref validation Add a positive test case and two negative test cases (valid data with invalid schema ref and invalid data with valid schema ref) --- .../fixtures/invalid-schema-v4-with-refs.json | 16 ++++++ test/fixtures/valid-schema-v4-with-refs.json | 16 ++++++ test/unit/validators/json-schema-test.coffee | 57 ++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/invalid-schema-v4-with-refs.json create mode 100644 test/fixtures/valid-schema-v4-with-refs.json diff --git a/test/fixtures/invalid-schema-v4-with-refs.json b/test/fixtures/invalid-schema-v4-with-refs.json new file mode 100644 index 00000000..a5250e85 --- /dev/null +++ b/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" + } + } +} diff --git a/test/fixtures/valid-schema-v4-with-refs.json b/test/fixtures/valid-schema-v4-with-refs.json new file mode 100644 index 00000000..85c77dc8 --- /dev/null +++ b/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" + } + } +} diff --git a/test/unit/validators/json-schema-test.coffee b/test/unit/validators/json-schema-test.coffee index 4da943a0..d3283b15 100644 --- a/test/unit/validators/json-schema-test.coffee +++ b/test/unit/validators/json-schema-test.coffee @@ -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 @@ -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 From ee7d7aa5190ab897fe704887b33d9098f64f57d4 Mon Sep 17 00:00:00 2001 From: Joao Andrade Date: Tue, 28 Feb 2017 16:21:09 -0800 Subject: [PATCH 2/2] feat: error on missing schema refs Reformat tv4's missing schema output to match the expected validation error format --- src/validators/json-schema.coffee | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/validators/json-schema.coffee b/src/validators/json-schema.coffee index 40c641f3..1892135d 100644 --- a/src/validators/json-schema.coffee +++ b/src/validators/json-schema.coffee @@ -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