houdin
Route-level file type validation for hapi parsed in-memory multipart/form-data
request payloads. Also works as a standalone module.
Table of Contents
Installation
Install via NPM.
$ npm install houdin
Usage
validate(payload, options)
Validates all Buffer
values in a payload
given a whitelist
of file types provided in the options
. Throws a joi-like ValidationError
if some file type is not allowed or unknown otherwise it returns the original payload.
Hapi
const Hapi = require('hapi');
const Houdin = require('houdin');
const server = new Hapi.Server({
routes: {
validate: {
options: {
whitelist: ['image/png']
}
}
}
});
server.route({
options: {
validate: {
failAction: (request, h, err) => {
throw err;
},
payload: Houdin.validate
},
payload: {
output: 'data',
parse: true
}
}
});
Standalone
const Houdin = require('houdin');
const options = { whitelist: ['image/png'] };
const png = Buffer.from('89504e470d0a1a0a', 'hex');
const payload = Houdin.validate({ file: png }, options);
console.log(payload);
const Houdin = require('houdin');
const options = { whitelist: ['image/png'] };
const gif = Buffer.from('474946383761', 'hex');
try {
Houdin.validate({ file: gif }, options);
}
catch (err) {
console.log(err);
}
Supported File Types
The same as file-type.