Skip to content

Commit

Permalink
Merge pull request #1 from dadi/feature/media-type
Browse files Browse the repository at this point in the history
Add Media type
  • Loading branch information
eduardoboucas committed Oct 26, 2018
2 parents dafdfa4 + 5dfefdc commit 69f1ed5
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const types = {
array: require('./types/array'),
boolean: require('./types/boolean'),
datetime: require('./types/datetime'),
media: require('./types/media'),
mixed: require('./types/mixed'),
number: require('./types/number'),
object: require('./types/object'),
Expand Down
2 changes: 1 addition & 1 deletion lib/shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports.isHexadecimal = value => {
return (typeof value === 'string') && /^[0-9A-F]+$/i.test(value)
return (typeof value === 'string') && /^[0-9A-F-]+$/i.test(value)
}
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.0.0",
"version": "1.1.0",
"description": "Validation package for DADI API",
"main": "index.js",
"directories": {
Expand Down
126 changes: 126 additions & 0 deletions test/types/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const should = require('should')
const request = require('supertest')

const media = require('./../../types/media')

const mockSchema = {
type: 'media'
}

describe('Media type', done => {
it('should export a function', () => {
(typeof media).should.eql('function')
})

describe('Single value', () => {
it('should reject if the input value is not a hexadecimal string or object with an hexadecimal _id', done => {
media({
schema: mockSchema,
value: 123456
}).catch(error => {
error.should.be.instanceOf(Error)

media({
schema: mockSchema,
value: {
someProperty: '123456'
}
}).catch(error => {
error.should.be.instanceOf(Error)

media({
schema: mockSchema,
value: {
_id: '123456ZY'
}
}).catch(error => {
error.should.be.instanceOf(Error)

done()
})
})
})
})

it('should resolve if the input value is a hexadecimal string', () => {
return media({
schema: mockSchema,
value: '5bd1c08a7a39d56eb0af7c1d'
})
})

it('should resolve if the input value is an object with an hexadecimal _id', () => {
return media({
schema: mockSchema,
value: {
_id: '5bd1c08a7a39d56eb0af7c1d'
}
})
})

it('should resolve if the input value is an object with an hexadecimal _id plus additional properties', () => {
return media({
schema: mockSchema,
value: {
_id: '5bd1c08a7a39d56eb0af7c1d',
altText: 'Lorem ipsum',
crop: [16, 32, 64, 128]
}
})
})
})

describe('Multi-value', () => {
it('should reject if the input array contains a value that is not a hexadecimal string or object with an hexadecimal _id', done => {
media({
schema: mockSchema,
value: ['5bd1c08a7a39d56eb0af7c1d', 123456]
}).catch(error => {
error.should.be.instanceOf(Error)

media({
schema: mockSchema,
value: [
'5bd1c08a7a39d56eb0af7c1d',
{
someProperty: '123456'
}
]
}).catch(error => {
error.should.be.instanceOf(Error)

media({
schema: mockSchema,
value: [
'5bd1c08a7a39d56eb0af7c1d',
{
_id: '123456ZY'
}
]
}).catch(error => {
error.should.be.instanceOf(Error)

done()
})
})
})
})

it('should resolve if the input array contains valid values', () => {
return media({
schema: mockSchema,
value: [
'5bd1c08a7a39d56eb0af7c1d',
{
_id: '5bd1c08a7a39d56eb0af7c1d'
},
{
_id: '5bd1c08a7a39d56eb0af7c1d',
altText: 'Lorem ipsum',
crop: [16, 32, 64, 128]
}
]
})
})
})
})
15 changes: 15 additions & 0 deletions types/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const {isHexadecimal} = require('./../lib/shared')
const ValidationError = require('./../lib/validation-error')

module.exports = ({schema, value}) => {
let normalisedValue = Array.isArray(value) ? value : [value]
let isCorrectType = normalisedValue.every(value => {
return isHexadecimal(value) || isHexadecimal(value._id)
})

if (!isCorrectType) {
return new ValidationError(schema).reject()
}

return Promise.resolve()
}

0 comments on commit 69f1ed5

Please sign in to comment.