dto-types
v0.0.2
Published
Express middleware for dto type checking based off of React PropTypes
Downloads
3
Readme
dto-types
DtoTypes is middleware for express based on PropTypes. PropTypes are an effective mechanism for keeping track of what a component should be receiving, so why not extend the idea to a DTOs (data transfer objects). I've found myself, instead writing custom validation for every api route I write. Hopefully this alleviates that by specifying the dto shapes only one time.
Installation
npm install --save dto-types
Use
import { DtoTypes, DtoValidator } from 'dto-types'; // ES6
const orderDtoType = {
total: DtoTypes.number.isRequired,
items: DtoTypes.arrayOf(DtoTypes.shape({
name: DtoTypes.string.isRequired,
price: DtoTypes.number.isRequired,
}).isRequired,
}
app.post('/orders', DtoValidator(orderDtoType), (req, res) => { // do stuff })