Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

express class validator

// using like this 
// this.router.post('/register', [validator(DTORegister)], this.registerUsersController)

import { validate, ValidationError } from 'class-validator'
import { StatusCodes as status } from 'http-status-codes'
import { Request, Response, NextFunction } from 'express'
import { OutgoingMessage } from 'http'

import { apiResponse } from '@helpers/helper.apiResponse'

export function validator(DataTransferObject: any) {
  return async function (req: Request, res: Response, next: NextFunction): Promise<OutgoingMessage> {
    const errors: ValidationError[] = await validate(Object.assign(new DataTransferObject(), req.body))
    const errorMessage = errors.map((val: ValidationError) => {
      return apiResponse(400, Object.values(val.constraints)[0])
    })

    if (errorMessage.length) {
      return res.status(status.BAD_REQUEST).json(errorMessage)
    }
    next()
  }
}
Comment

express class validator

import { validate, ValidationError } from 'class-validator'
import { ClassConstructor, plainToClass } from 'class-transformer'
import { StatusCodes as status } from 'http-status-codes'
import { Request, Response, NextFunction } from 'express'
import { OutgoingMessage } from 'http'

import { apiResponse } from '@helpers/helper.apiResponse'

export function validator(MetaType: ClassConstructor<any>) {
  return async function (req: Request, res: Response, next: NextFunction): Promise<OutgoingMessage> {
    let property: Record<string, any> = {}
    Object.assign(property, req.body, req.params, req.query)

    const object: Record<string, any> = plainToClass(MetaType, property)
    const errorsResponse: ValidationError[] = await validate(object)

    const errorMessage = errorsResponse.map((val: ValidationError) => apiResponse(400, Object.values(val.constraints)[0]))
    if (errorMessage.length) {
      return res.status(status.BAD_REQUEST).json({ errors: errorMessage })
    }
    next()
  }
}
Comment

express class validator

// using like this 
// this.router.post('/register', [validator([DTORegister])], this.registerUsersController)

export function validator(DataTransferObject: any) {
  return async function (req: Request, res: Response, next: NextFunction): Promise<OutgoingMessage> {
    let errors: any = []

    DataTransferObject.forEach(async (DTO: any) => {
      const property: Record<string, any> = {}
      Object.assign(property, req.body, req.params, req.query)
      const errorsResponse: ValidationError[] = await validate(Object.assign(new DTO(), property))
      errors.push(errorsResponse)
    })

    const errorMessage = errors.map((val: ValidationError) => apiResponse(400, Object.values(val.constraints)[0]))
    if (errorMessage.length) {
      return res.status(status.BAD_REQUEST).json(errorMessage)
    }
    next()
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: set constraints for UIView swift 
Typescript :: how to compile ts in cmd 
Typescript :: axios typescript get 
Typescript :: readonly in typescript 
Typescript :: typescript function 
Typescript :: file reader with promise 
Typescript :: rewrite requests htaccess 
Typescript :: typescript string 
Typescript :: jest not tocontain 
Typescript :: No type arguments expected for interface Callback 
Typescript :: angular how to use observable object async 
Typescript :: typescript to java converter 
Typescript :: typescript discriminated unions 
Typescript :: is missing in props validation typescript 
Typescript :: How to use the Generic Type Format for Arrays in Typescript 
Typescript :: how to take inputs in one line in c 
Typescript :: datasets in python github 
Typescript :: dynamic key interface typescript 
Typescript :: cluster list values python 
Typescript :: check if all array elements match closure swift 
Typescript :: stipe elements angular.js 
Typescript :: how t make fireball roblox or lua 
Typescript :: all default datasets in seaborn 
Typescript :: game object attributes 
Typescript :: write getter angular 
Typescript :: five elements in the finger 
Typescript :: remove dots from image python 
Typescript :: how to execute more commands scripts package.json 
Typescript :: What will be the result of the cp /etc/hosts . command? 
Typescript :: Returns number of valuesDisplays the number in brackets of return value 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =