Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
25 changes: 25 additions & 0 deletions test/types/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
}
]
})
})
})
})
31 changes: 31 additions & 0 deletions test/validate-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ describe('validateDocument', () => {
})
})

it('should not reject when a non-required field is set to null', () => {
let validator = new Validator()

return validator.validateDocument({
document: {
title: 'hello world',
revision: null,
publishedAt: null
},
schema: mockSchema
})
})

it('should not reject when a required field is missing from the payload and `isUpdate: true`', () => {
let validator = new Validator()

Expand All @@ -204,6 +217,24 @@ describe('validateDocument', () => {
})
})

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 reject when a field fails the validation rules and `isUpdate: true`', done => {
let validator = new Validator()

Expand Down
2 changes: 2 additions & 0 deletions types/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down