diff --git a/lib/validators/json-schema-next.js b/lib/validators/json-schema-next.js index 905ecd07..480f4ec5 100644 --- a/lib/validators/json-schema-next.js +++ b/lib/validators/json-schema-next.js @@ -244,9 +244,9 @@ class JsonSchemaValidator { switch (keyword) { case 'type': - return `At '${pointer}' Invalid type: ${typeof data} (expected ${ - params.type - })`; + return `At '${pointer}' Invalid type: ${ + data === null ? null : typeof data + } (expected ${params.type})`; case 'required': return `At '${pointer}' Missing required property: ${last(property)}`; diff --git a/test/unit/units/validateBody.test.js b/test/unit/units/validateBody.test.js index 09611e71..bd013a17 100644 --- a/test/unit/units/validateBody.test.js +++ b/test/unit/units/validateBody.test.js @@ -353,6 +353,45 @@ describe('validateBody', () => { }); }); }); + + describe('with non-matching body types', () => { + const result = validateBody( + { + bodySchema: { + properties: { + firstName: { + type: 'string' + } + } + } + }, + { + body: '{ "firstName": null }' + } + ); + + it('marks field as invalid', () => { + expect(result).to.not.be.valid; + }); + + it('has "json" kind', () => { + expect(result).to.have.kind('json'); + }); + + describe('produces an error', () => { + it('exactly one error', () => { + expect(result).to.have.errors.lengthOf(1); + }); + + it('has explanatory message', () => { + expect(result) + .to.have.errorAtIndex(0) + .withMessage( + `At '/firstName' Invalid type: null (expected string)` + ); + }); + }); + }); }); }); }); diff --git a/test/unit/validators/json-schema.test.js b/test/unit/validators/json-schema.test.js index 92c93be3..a6a3dd64 100644 --- a/test/unit/validators/json-schema.test.js +++ b/test/unit/validators/json-schema.test.js @@ -109,8 +109,6 @@ describe('JsonSchema', () => { }); }); }); - - // shared.shouldBehaveLikeAmandaToGavel(new JsonSchemaValidator('{}')); }); describe('when validation performed on actual empty object', () => {