The original one is using my previous company email, I am not able to update this. So, I move this to my personal account and rename to nest-yup
.
Integrate Yup
with Nest.js
.
- Create your own
yup schema
. - Use the decorator
UseSchema
.
import { UseSchema } from 'nest-yup';
For example:
import * as yup from 'yup';
import { UseSchema } from 'nest-yup';
export const authSchema = yup.object({
username: yup.string().required().min(4).max(20),
password: yup
.string()
.required()
.min(8)
.max(20)
.matches(
/((?=.*\d)|(?=.*\W))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/,
'password too weak',
),
});
@UseSchema(authSchema)
export class AuthCredentialsDto {
username: string;
password: string;
}
import { YupValidationPipe } from 'nest-yup';
For example:
@Post('/signup')
signUp(
@Body(YupValidationPipe)
authCredentialsDto: AuthCredentialsDto,
): Promise<void> {
return this.authService.signUp(authCredentialsDto);
}
npm i nest-yup
Don't forget to install yup
as well.
npm i yup
npm i -D @types/yup
Please go to example
folder.
npm run start:dev
or running via docker
docker-compose up
- @c1495616js - Idea & Initial work