express class validator express class validator express class validator
The solution for “express class validator express class validator express class validator” can be found here. The following code will assist you in solving the problem.
// 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 {
let errors: any = []
DataTransferObject.forEach(async (DTO: any) => {
const property: Record = {}
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()
}
}// 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 {
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()
}
}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) {
return async function (req: Request, res: Response, next: NextFunction): Promise {
let property: Record = {}
Object.assign(property, req.body, req.params, req.query)
const object: Record = 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()
}
}
More questions on [categories-list]
- tss from gene granges
- ixl ansers ixl ansers
- get coin prices node-binance
- how to setup netflix workflow worker
- spritesheets in pyqt spritesheets in pyqt
- cahokia mounds pictures cahokia mounds pictures cahokia mounds pictures
- python 2 decimal places how to get decimal part of a double in python set number of decimals python
- how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python
- haskell get specific elements of a String
- vb net code snippets for storing password
- error TS2307: Cannot find module ‘@ngx-meta/core’.
- inline scripts encapsulated in tags