Skip to content

c1495616js/nest-yup

Repository files navigation

Nest Logo

Nest.js + Yup

Status


Alert

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.

📝 Table of Contents

🧐 About

Integrate Yup with Nest.js.

🎥 Demo / Working

🎈 Usage

In DTO file

  • 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;
}

In Controller file

import { YupValidationPipe } from 'nest-yup';

For example:

 @Post('/signup')
  signUp(
    @Body(YupValidationPipe)
    authCredentialsDto: AuthCredentialsDto,
  ): Promise<void> {
    return this.authService.signUp(authCredentialsDto);
  }

Installing

npm i nest-yup

Don't forget to install yup as well.

npm i yup
npm i -D @types/yup

Quick Example

Please go to example folder.

npm run start:dev

or running via docker

docker-compose up

✍️ Authors

🎉 Acknowledgements

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published