npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-yup-validator

v1.0.14

Published

Nest js is an awesome ndoejs framework for building scalable aplications with clean and reusable code ( my favorite). However it has some problems with its current validation system. While it can be easy to use an intuitive for some people, for other it

Downloads

297

Readme

Yup Validation for nest

Introduction

The problem

Nest js is an awesome ndoejs framework for building scalable aplications with clean and reusable code ( my favorite). However it has some problems with its current validation system. While it can be easy to use an intuitive for some people, for other it can be frustrating o limiting. The actual validation system is really good ( im my opinion ), but nestjs lack of alternatives for requisition validation.

The idea

Yup is a schema builder for validating and parsing objects. Its simple, lightweight and easy to use. Even that it may not be so powerfull as class-validator, it may be helpfull in some cases, making easy and fast to validate query string and body objects from requisitions.

The purpose

Ofer an alternative to class-validator with a package to integrate NestJS and Yup, wich may not be as powerfull as the current validation system but can it can be really handfull and simple to use. Also offer some manual validation option, with in some cases may be usefull.

How to use

Installation

In you project directory

$ npm install nestjs-yup-validator --save
Creating a validation
With schema:
    // Creating  a yup schema
    export const SaveUserValidation  = yup.object({
        username: yup.string().required().min(3).max(15),
        mail: yup.string().required().email(),
        password: yup.string().required().min(8).max(16),
        age: yup.number().min(1)
    })

Optional DTO usage (for typescript)

    // request dto
    export class SaveUserReqDTO {
        username: string
        mail: string
        password: string
        age: number
    }
    
    // validation schema
    export const SaveUserValidation  = yup.object<SaveUserReqDTO>({
        username: yup.string().required().min(3).max(15),
        mail: yup.string().required().email(),
        password: yup.string().required().min(8).max(16),
        age: yup.number().min(1)
    })
With custom function:
    export function SaveUserValidation(body) {
        validateUserObject(body)
        const validatedUserObject = transformUserObject()
        
        // the returned value will be passed to the object
        return {
            requestTime: new Date().toString(),
            ...validatedUserObject
        }
    }

Validating

Applying on body requests

You may apply the validation using a Decorator on the controller. Use BodyValidator (for post, put, fetch and delete) or QueryValidator (for get) with the schema, a function or even both of them.

See the example bellow:

      @Post('post')
      createPost(@BodyValidator([SavePostValidation]) body: SaveUserReqDTO) {
        const newPost  = this.postService.save(body)
      
        return newPost
      }
      
      @Get('querystring-validation') 
      queryValidation(@QueryValidator([ListThisValidation]) listFilter: ListThigsDTO) {
        return this.thisgsService.find(listFilter)
      }
      
      @Post('some-complex-request-validation') 
      someComplexValidation(@QueryValidator([ValidationSchema, validationFunction, anotheValidationFunction]) listFilter: ListThigsDTO) {
        return this.thisgsService.find(listFilter)
      }

Notes

  • This package only add a easy way to integrate yup validation with NestJS validation. All the validation and hard work is made by Yup lib.
  • The aim of this package its not to replace body-validator. You may still use body-validator and Nest-Yup-Validation in the same aplication.

Information

  • Check Yup documentation for more info about validation here
  • Check NestJS documentation about Custom route decorators here

License

MIT