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

How to use if inside ParamSchema? #1266

Closed
dfyz011 opened this issue Dec 2, 2023 · 1 comment
Closed

How to use if inside ParamSchema? #1266

dfyz011 opened this issue Dec 2, 2023 · 1 comment

Comments

@dfyz011
Copy link

dfyz011 commented Dec 2, 2023

Hello.
I have such construction in couple of places

import type { Schema } from 'express-validator';

export const schema: Schema = {
  orderId: {
    // @ts-ignore TODO: no type in express-validator but works
    if: {
      options: (v, { req }) =>
        condition
          ? !v
          : false,
    },
    isInt: true,
    toInt: true,
  },
};

It worked before on version 6.2.0, but as you can see I used @ts-ignore because without it had error "Object literal may only specify known properties, and 'if' does not exist in type 'ParamSchema"

But now, after I updated express-validator to 7.0.1 this not working anymore and also I have error inside my console after build "express-validator: schema of "orderId" has unknown validator/sanitizer "if""

That why i decided to finally create issue and ask: Is it me doing something wrong?
Link to documentation https://express-validator.github.io/docs/api/check-schema#if
Seems like I should use if only inside validator, like this?

import type { Schema } from 'express-validator';

export const schema: Schema = {
  orderId: {
    isInt: {
      if: (v, { req }) =>
        condition
          ? !v
          : false,
    },
    toInt: true,
  },
};

But will it ignore everything(other validators, customs, sanitizers) after "isInt" key or only validators?

@fedeci
Copy link
Member

fedeci commented Feb 13, 2024

That why i decided to finally create issue and ask: Is it me doing something wrong? Link to documentation express-validator.github.io/docs/api/check-schema#if Seems like I should use if only inside validator, like this?

import type { Schema } from 'express-validator';

export const schema: Schema = {
  orderId: {
    isInt: {
      if: (v, { req }) =>
        condition
          ? !v
          : false,
    },
    toInt: true,
  },
};

But will it ignore everything(other validators, customs, sanitizers) after "isInt" key or only validators?

Yes it will ignore everything after isInt. We decided to move it because now you can add more than one if and place it accurately before a chosen validator.
Behind the scenes checkSchema behaves like a validation chain and validators are run in the same order they are added to the object. I know this is a little bit counter-intuitive because order in objects usually doesn't matter, but in this case it does.

@fedeci fedeci closed this as completed Feb 13, 2024
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