@ezy/coerce-middleware
v2.1.1
Published
coerce any property in req object
Downloads
19
Maintainers
Readme
Why
Validation logic become simpler when it's possible to expect "typed" datas, but datas parsed from url like req.query or req.params are strings.
Install
npm i @ezy/coerce-middleware
Usage
Common
import { coerce } from '@ezy/coerce-middleware';
import express from 'express';
const app = express();
app.use(coerce('query'));
app.get('/', (req, res) => {
res.send(JSON.stringify(req.query));
});
app.listen(3000);
// GET /?id=30&is_admin=true&name=awesome%20title
// => {
// name: 'awesome title',
// id: 30,
// is_admin: true
// }
use a custom coercePrimitive function
import { coerce } from '@ezy/coerce-middleware';
import express from 'express';
const app = express();
app.use(coerce('query', value => '(^^)'));
app.get('/', (req, res) => {
res.send(JSON.stringify(req.query));
});
app.listen(3000);
// GET /?id=30&is_admin=true&nested[name]=awesome%20title
// => {
// nested: {
// name: '(^^)'
// },
// id: '(^^)',
// is_admin: '(^^)'
// }