afraid
v1.0.0-alpha.11
Published
Express middlewares for validating incoming data with type inference
Downloads
4
Readme
afraid
😧 Afraid?! You don't need to be: Incoming data of your express route is validated! Type inference included!
Installation
npm install afraid --save --no-optional
Usage
import {query, f, fail} from 'afraid';
import * as express from 'express';
const app = express();
app.get('/users', [
query(
f('limit').int(),
f('offset').int(),
f('filters').string().array().opt(),
),
fail,
], (req, res, next) => {
// ...
});
Using classes for validation and transformation
Installation
Omitting --no-optional
will install required packages class-transformer
and reflect-metadata
automatically
npm install afraid --save
Configuration
The following flags in tsconfig.json
:
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
Usage
import {query, Field, IsInt, fail} from 'afraid'
import * as express from 'express';
const app = express();
class UserDTO {
@Field name: string;
@IsInt() @Field age: number;
}
app.post('/users', [
body(UserDTO),
fail,
], (req, res, next) => {
// ...
});
Swagger documentation
Use afraid-swagger to create swagger documentation from afraid middlewares.