Socket
Socket
Sign inDemoInstall

nestjs-yup-validator

Package Overview
Dependencies
4
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nestjs-yup-validator

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


Version published
Weekly downloads
7
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 03 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc