Express Validator: To check if a Body Param is one of the value of a given object

747 Views Asked by At

I have made an object with Erroneous values, which looks like:

const ERROR_CHECK = {
    "null" : null,
    "undefined" : undefined,
    "Error" :  Error,
    "ErrorOb" : new Error,
    "Empty": "*"
}

I want to check that if a body param (say name) has any of these values it should throw an Error. So far, I have tried this :

const validator = [
       check('name')
        .optional()
        .bail()
        .notEmpty().withMessage("Invalid Company Id")
        .bail()
        .matches(/^[A-Za-z\s]+$/).withMessage("Invalid Company Name") //String to mach Alphabetical and spaces only
        .bail()
        .custom((value, { req }) => Object.values(ERROR_CHECK).includes(value)).withMessage("Invalid Company Name")
        .bail()
        .custom((value, { req }) => value in ERROR_CHECK).withMessage("Invalid Company Name")
        .bail()
        .trim().isString().withMessage("Invalid Company Name") 
]

But still when null, undefined, Error etc are passed it does not throw error. What should I add/subtract from this code to make it work?

0

There are 0 best solutions below