nest.js + yup validation
npm install nestjs-yupYup with Nest.js.
yup schema.
UseSchema.
ts
import { UseSchema } from 'nestjs-yup';
`
For example:
`ts
import * as yup from 'yup';
import { UseSchema } from 'nestjs-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;
}
`
#### In Controller file
`ts
import { YupValidationPipe } from 'nestjs-yup';
`
For example:
`ts
@Post('/signup')
signUp(
@Body(YupValidationPipe)
authCredentialsDto: AuthCredentialsDto,
): Promise {
return this.authService.signUp(authCredentialsDto);
}
`
$3
`
npm i nestjs-yup
`
Don't forget to install yup as well.
`
npm i yup
npm i -D @types/yup
`
$3
Please go to example folder.
`
npm run start:dev
`
or running via docker
`
docker-compose up
``