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

Usage of exist, if in checkSchema #1244

Open
yerycs opened this issue Sep 12, 2023 · 0 comments
Open

Usage of exist, if in checkSchema #1244

yerycs opened this issue Sep 12, 2023 · 0 comments

Comments

@yerycs
Copy link

yerycs commented Sep 12, 2023

I am implementing conditional validation. Here is the payload.

{
  "address": {
    "address1": ""
  }
}

address field is optional. Sub field address1 should be required when address exists.

Here is my checkSchema validator.

checkSchema({
  "address": {
    isObject: {
      options: { strict: true },
      bail: true,
    },
    optional: true,
  },
  "address.address1": {
    exists: {
      if: body("address").notEmpty(),
    }
  }
})

This validator is working fine.

The problem is matchedData after pass validator when there isn't address in payload, address and address1 is generated.

So, I passed empty object {}. But the matchedData is following value.

{
  "address": {
    "address1": undefined
  }
}

The matchedData should be empty object also. How can I solve it with correct validation?

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

1 participant