nest-yup
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i nest-yup

Weekly Downloads

112

Version

1.0.4

License

ISC

Unpacked Size

12.6 kB

Total Files

19

Last publish

Collaborators

  • c1495616js