joi-dataURI
v1.0.0
Published
Provides a Joi schema for dataURIs (Adds joi.dataURI()[.mediatype([mediatype])][.base64()])
Downloads
17
Readme
Adds a validator for dataURIs to Joi ( joi.dataURI()[.mediatype([mediatype])][.base64()] ).
a quick example
const joiDataUri = require('joi-dataURI')
const Joi = require('joi').extend(joiDataUri)
let exampleData = (new Buffer('hello world')).toString('base64')
let mimetype = 'text/plain'
let exampleDataURI = 'data:' + mimetype + ';base64,' + exampleData
Joi.dataURI().validate(exampleDataURI)
/**
{ error: null,
value: 'data:text/plain;base64,aGVsbG8gd29ybGQ=' }
**/
Joi.dataURI().base64().mediatype('text/plain').validate(exampleDataURI)
/**
{ error: null,
value: 'data:text/plain;base64,aGVsbG8gd29ybGQ=' }
**/
As expected, a valid data URI (matching our constraints) passes!
But the same will be rejected if we specify the wrong mediatype:
Joi.dataURI().base64().mediatype('text/csv').validate(exampleDataURI)
/**
{ error:
{ ValidationError: "value" needs to have provided mediatype
at Object.exports.process (/SomePath/joi-dataURI/node_modules/joi/lib/errors.js:154:19)
at _validateWithOptions (/SomePath/joi-dataURI/node_modules/joi/lib/any.js:601:31)
at validate (/SomePath/joi-dataURI/node_modules/joi/lib/any.js:616:21)
at repl:1:46
at REPLServer.defaultEval (repl.js:272:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:441:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
isJoi: true,
name: 'ValidationError',
details: [ [Object] ],
_object: 'data:text/plain;base64,aGVsbG8gd29ybGQ=',
annotate: [Function] },
value: 'data:text/plain;base64,aGVsbG8gd29ybGQ=' }
**/
tests
More examples are in tests.
After installing, run tests with npm test
.