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

Feature/report failed conditions #1130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

amagid
Copy link

@amagid amagid commented Feb 13, 2022

Description

Closes #1124

Attaching failed validation condition on calls to context.addError. This PR adds a nonenumerable property "condition" to errors resulting from failed validation. This property is nonenumerable and therefore should not affect any existing express APIs that return output from express-validator. However, it can be used to determine which validation condition failed by reading error output and checking the condition property directly. This change maintains express-validator's goal of being unopinionated with regard to error messages while also allowing users to automatically generate human-readable error messages if they want to.

For example, if an input fails an isLength validation, contextError.condition.constructor.name === 'isLength', so I would be able to generate an error message referencing the specific condition that failed rather than a generic message.

To preserve existing functionality with minimal impact, the condition property is always marked optional in the addError function signature.

To-do list

  • I have added tests for what I changed.
  • This pull request is ready to merge.

@gustavohenke gustavohenke force-pushed the master branch 2 times, most recently from 3177f64 to 6e160b1 Compare April 7, 2023 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include which validator failed in errors.mapped()
1 participant