Skip to content

Instantly share code, notes, and snippets.

@appletreeat56
Created October 25, 2022 14:21
Show Gist options
  • Save appletreeat56/b2545b1b22ef1835f9e344dc384c5a0d to your computer and use it in GitHub Desktop.
Save appletreeat56/b2545b1b22ef1835f9e344dc384c5a0d to your computer and use it in GitHub Desktop.
import {
Result,
ValidationError,
validationResult,
} from 'express-validator'
import { Request, Response, NextFunction } from 'express'
export const validateCharity = async (
req: Request,
res: Response,
next: NextFunction
) => {
const errors: Result = validationResult(req)
const errorMessages: ValidationError[] = errors.array()
//Check if an organisation with a name & charity number already exists
await validateCharityNumber(req, errorMessages)
if (!errorMessages.length) return next()
else return res.status(422).json({ errors: errorMessages })
}
const validateCharityNumber = async (
req: Request,
errorMessages: ValidationError[]
) => {
let charity;
try {
//insert business logic to check of charity details
} catch (error) {
console.info('charity not found')
}
if (charity.length) {
errorMessages.push({
value: '',
msg: `A chaity with a regitration code '${req.body.charityNumber}' already exists`,
param: 'email',
location: 'body',
})
}
return charity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment