-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dadi/feature/media-type
Add Media type
- Loading branch information
Showing
5 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
] | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |