Created
October 25, 2022 14:21
-
-
Save appletreeat56/b2545b1b22ef1835f9e344dc384c5a0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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