From cb21d5d54cce4c51873a8490bf70cc3e125f0369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 29 Nov 2018 11:10:16 +0000 Subject: [PATCH 1/3] refactor: treat null values as a special case --- index.js | 15 +++++++++++++++ test/types/media.js | 25 +++++++++++++++++++++++++ test/validate-document.js | 31 +++++++++++++++++++++++++++++++ types/media.js | 2 ++ 4 files changed, 73 insertions(+) diff --git a/index.js b/index.js index 7ff7f47..e7a4f5a 100644 --- a/index.js +++ b/index.js @@ -48,6 +48,21 @@ class Validator { }) } + // We treat null values as a special case. If the field is required, + // we reject with the `ERROR_REQUIRED` code, because technically the + // field is not set. If the field is not required, we accept the value. + if (value === null) { + if (fieldSchema.required) { + return errors.push({ + code: 'ERROR_REQUIRED', + field, + message: 'must be specified' + }) + } + + return + } + chain = chain.then(() => { return this.validateValue({ schema: fieldSchema, diff --git a/test/types/media.js b/test/types/media.js index b3643d9..b191fa9 100644 --- a/test/types/media.js +++ b/test/types/media.js @@ -42,6 +42,13 @@ describe('Media type', done => { }) }) + it('should resolve if the input value is null', () => { + return media({ + schema: mockSchema, + value: null + }) + }) + it('should resolve if the input value is a hexadecimal string', () => { return media({ schema: mockSchema, @@ -122,5 +129,23 @@ describe('Media type', done => { ] }) }) + + it('should resolve if the input array contains null values', () => { + return media({ + schema: mockSchema, + value: [ + '5bd1c08a7a39d56eb0af7c1d', + { + _id: '5bd1c08a7a39d56eb0af7c1d' + }, + null, + { + _id: '5bd1c08a7a39d56eb0af7c1d', + altText: 'Lorem ipsum', + crop: [16, 32, 64, 128] + } + ] + }) + }) }) }) diff --git a/test/validate-document.js b/test/validate-document.js index b4d313b..0fba08b 100644 --- a/test/validate-document.js +++ b/test/validate-document.js @@ -183,6 +183,37 @@ describe('validateDocument', () => { it('should not reject when a non-required field is missing from the payload', () => { let validator = new Validator() + return validator.validateDocument({ + document: { + title: 'hello world', + revision: null, + publishedAt: null + }, + schema: mockSchema + }) + }) + + it('should reject when a required field is set to null', done => { + let validator = new Validator() + + validator.validateDocument({ + document: { + title: null + }, + schema: mockSchema + }).catch(error => { + error.should.be.instanceof(Array) + error.length.should.eql(1) + error[0].code.should.eql('ERROR_REQUIRED') + error[0].message.should.be.instanceof(String) + + done() + }) + }) + + it('should not reject when a non-required field is set to null', () => { + let validator = new Validator() + return validator.validateDocument({ document: { title: 'hello world', diff --git a/types/media.js b/types/media.js index 247054e..d8fd7ab 100644 --- a/types/media.js +++ b/types/media.js @@ -4,6 +4,8 @@ const ValidationError = require('./../lib/validation-error') module.exports = ({schema, value}) => { let normalisedValue = Array.isArray(value) ? value : [value] let isCorrectType = normalisedValue.every(value => { + if (value === null) return true + return isHexadecimal(value) || isHexadecimal(value._id) }) From a4f7b5794cc3d60e943788c93c559c1c10b8daf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 29 Nov 2018 11:11:22 +0000 Subject: [PATCH 2/3] chore: bump version --- CHANGELOG.md | 8 +++++++- package.json | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2778db9..c721f11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.1.1] (2018-11-29) + +### Fixed + +- [#2](https://github.com/dadi/api-validator/issues/2): treat `null` values as a special case + ## [1.1.0] (2018-10-26) ### Changed -- [#1](https://github.com/dadi/api-validator/pull/1): Add support for `Media` type. +- [#1](https://github.com/dadi/api-validator/pull/1): add support for `Media` type. diff --git a/package.json b/package.json index 1bfc304..c31f771 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dadi/api-validator", - "version": "1.1.0", + "version": "1.1.1", "description": "Validation package for DADI API", "main": "index.js", "directories": { From d97a9df44c63620980207c5b7f92aff95a3d8440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 29 Nov 2018 11:13:54 +0000 Subject: [PATCH 3/3] test: fix naming in tests --- test/validate-document.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/validate-document.js b/test/validate-document.js index 0fba08b..bf6d00c 100644 --- a/test/validate-document.js +++ b/test/validate-document.js @@ -186,52 +186,52 @@ describe('validateDocument', () => { return validator.validateDocument({ document: { title: 'hello world', - revision: null, - publishedAt: null + revision: 12 }, schema: mockSchema }) }) - it('should reject when a required field is set to null', done => { + it('should not reject when a non-required field is set to null', () => { let validator = new Validator() - validator.validateDocument({ + return validator.validateDocument({ document: { - title: null + title: 'hello world', + revision: null, + publishedAt: null }, schema: mockSchema - }).catch(error => { - error.should.be.instanceof(Array) - error.length.should.eql(1) - error[0].code.should.eql('ERROR_REQUIRED') - error[0].message.should.be.instanceof(String) - - done() }) }) - it('should not reject when a non-required field is set to null', () => { + it('should not reject when a required field is missing from the payload and `isUpdate: true`', () => { let validator = new Validator() return validator.validateDocument({ document: { - title: 'hello world', revision: 12 }, + isUpdate: true, schema: mockSchema }) }) - it('should not reject when a required field is missing from the payload and `isUpdate: true`', () => { + it('should reject when a required field is set to null', done => { let validator = new Validator() - return validator.validateDocument({ + validator.validateDocument({ document: { - revision: 12 + title: null }, - isUpdate: true, schema: mockSchema + }).catch(error => { + error.should.be.instanceof(Array) + error.length.should.eql(1) + error[0].code.should.eql('ERROR_REQUIRED') + error[0].message.should.be.instanceof(String) + + done() }) })