express-superstruct
An express validation middleware that uses superstruct
- Replaces the incoming
req.body
,req.query
, with the validated result, original values are kept inreq._body
/req._query
- Express error middleware will be executed upon validation error
Install
npm install express-superstruct
Usage
const validate = ;const express = ;const bodyParser = ;const app = ; // without req.body/req.query filled, nothing will be validated.app; app; app;
Advanced usage
Besides superstruct's built-in types, you can build custom ones!
const validate superstruct struct = ;/* ... */ const types = ; const customStruct = ; const schema = email: 'email' password: 'password' captcha: 'boolean?' // Optional field; app; app;
You can pass a more complex Struct too!
const schema = name: 'string & !empty' // !empty may be another custom type! emails: struct; app; /* ... */
Default values are supported:
const schema = name: 'string & !empty' // !empty may be another custom type! emails: struct foo: struct foo: 'bar' ; app; /* ... */
Handling validation error
const validate ValidationError = ;const express = ;const app = ; app; app;