https://github.com/robinbuschmann/afraid
Express middlewares for validating incoming data with type inference
https://github.com/robinbuschmann/afraid
Last synced: 9 months ago
JSON representation
Express middlewares for validating incoming data with type inference
- Host: GitHub
- URL: https://github.com/robinbuschmann/afraid
- Owner: RobinBuschmann
- Created: 2018-07-24T17:24:27.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-08T14:24:24.000Z (over 7 years ago)
- Last Synced: 2025-03-18T21:50:18.494Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 976 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/afraid)
[](https://travis-ci.org/RobinBuschmann/afraid)
# afraid
> 😧 Afraid?! You don't need to be: Incoming data of your express route is validated!
Type inference included!

- [Installation](#installation)
- [Usage](#usage)
- [Usage with classes](#using-classes-for-validation-and-transformation)
- [Generating swagger documentation](#swagger-documentation)
## Installation
```bash
npm install afraid --save --no-optional
```
## Usage
```typescript
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`:
```json
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
```
#### Usage
```typescript
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](https://github.com/RobinBuschmann/afraid-swagger) to create swagger documentation from afraid
middlewares.