https://github.com/jbpionnier/joi-decorator
Decorator based interface for Joi
https://github.com/jbpionnier/joi-decorator
decorator joi validator
Last synced: 2 months ago
JSON representation
Decorator based interface for Joi
- Host: GitHub
- URL: https://github.com/jbpionnier/joi-decorator
- Owner: jbpionnier
- License: mit
- Created: 2021-04-05T19:45:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-26T21:32:38.000Z (about 1 year ago)
- Last Synced: 2025-05-13T13:19:13.921Z (5 months ago)
- Topics: decorator, joi, validator
- Language: TypeScript
- Homepage:
- Size: 172 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# joi-decorator
Decorator based interface for [Joi](https://www.npmjs.com/package/joi).
## Installation
```sh
npm install joi-decorator reflect-metadata
```## The basics
First you need to declare an entity:
```ts
import { mustBe, a } from "joi-decorator";export class User {
@mustBe(a.string().alphanum().min(3).max(30).required())
username: string
@mustBe(a.string().regex(/^[a-zA-Z0-9]{3,30}$/))
password: string
@mustBe([a.string(), a.number()])
accessToken: string | number
@mustBe(a.number().integer().min(1900).max(2013))
birthyear: number
@mustBe(a.string().email())
email: string
constructor(
username: string,
password: string,
access_token: string | number,
birthyear: number,
email: string
) {
this.username = username
this.password = password
this.accessToken = accessToken
this.birthyear = birthyear
this.email = email
}
}
```Then you can validate the entity instances:
#### Example 1: Valid entity
```ts
import { validate } from "joi-decorator";
import { User } from "./entities/user";const userInput = new User(
"root",
"secret",
"token",
1989,
"test@test.com"
);const userValid = validate(userInput);
```---
## License
Inspired by [zafiro-validators](https://github.com/ZafiroJS/zafiro-validators/).
[MIT License](LICENSE)
Copyright (c) Jean-Baptiste Pionnier