Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Function to generate validators for each field #1235

Open
JwngmaBasumatary opened this issue Jul 18, 2023 · 1 comment
Open

Use a Function to generate validators for each field #1235

JwngmaBasumatary opened this issue Jul 18, 2023 · 1 comment

Comments

@JwngmaBasumatary
Copy link

Hello, So I am working in a large project and I'm tired of writing the same validator code everytime so i created a few functions which will return the validation logic when i call them and i can re-use them wherever i want.

For example, suppose this is my function :

const stringValidate = (source, text, optional) => {
  switch (source) {
    case "body":
      if (optional) {
        return body(text)
          .optional()
          .isString()
          .withMessage(`${text} must be String`)
          .trim();
      }
      let val = body(text)
        .notEmpty()
        .withMessage(`${text} Required`)
        .bail()
        .isString()
        .withMessage(`${text} must be String`)
        .trim();
      console.log("val", val);
      return val;
    case "query":
      if (optional) {
        return query(text)
          .optional()
          .isString()
          .withMessage(`${text} must be String`)
          .trim();
      }
      return query(text)
        .notEmpty()
        .withMessage(`${text} Required`)
        .bail()
        .isString()
        .withMessage(`${text} must be String`)
        .trim();
    case "param":
      if (optional) {
        return param(text)
          .optional()
          .isString()
          .withMessage(`${text} must be String`)
          .trim();
      }
      return param(text)
        .notEmpty()
        .withMessage(`${text} Required`)
        .bail()
        .isString()
        .withMessage(`${text} must be String`)
        .trim();
    default:
      console.log(`Default validator case for ${text} in ${source}`);
  }
};

This function will return a validation logic for any string based validation. The function takes 3 arguments :

  1. Source : source is either body, query or param
  2. Text : The name of the field
  3. Optional : Boolean, true if field is optional and vice-versa.

And this function I am trying to use in a route call like this

router.post(
  "",
  [
    stringValidate("body", "name", false),
  ],
  //   primaryAdminCheck,
  createAdmin
);

But the above code gives me an error

InternalServerError: argument #3 unsupported type boolean

So My question is if its possible what I am trying to do? Or do i have to look for a different method

@gustavohenke
Copy link
Member

Hi,
I'm not sure where your message is originating from. Most likely not from express-validator. Can you create a small reproduction repository?

On another note, you can make use of buildCheckFunction to make your function way smaller.

const stringValidate = (source, text, optional) => {
  const validateFn = buildCheckFunction([source]);
  const chain = validateFn(text);
  if (optional) {
    chain.optional();
  } else {
    chain.notEmpty().withMessage(`${text} required`).bail();
  }
  return chain
    .isString()
    .withMessage(`${text} must be String`)
    .trim();
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants