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

type string: alpha and optional option combinations #256

Open
sajcics opened this issue Jul 15, 2021 · 5 comments
Open

type string: alpha and optional option combinations #256

sajcics opened this issue Jul 15, 2021 · 5 comments

Comments

@sajcics
Copy link

sajcics commented Jul 15, 2021

WHAT?
When I define alpha parameter in the optional field, the validator throws error.

EXAMPLE:
Here is an example how I defined rule for name field.
name: { type: 'string', optional: true, alpha: true, trimLeft: true },

the validator throws error field must be an alphabetic string. If I remove alpha then validation on empty field works.

EXPECTING:
When optional is set then alpha will not validate on empty string.

VERSION:
latest: 1.11.1
nodejs: 14

@sajcics sajcics changed the title alpha and optional combinations type string: alpha and optional option combinations Jul 15, 2021
@erfanium
Copy link
Collaborator

It's expected, empty string is not a valid alpha pattern

@sajcics
Copy link
Author

sajcics commented Jul 15, 2021

yes, but I have an optional set to true, which means that alpha should not be applied if string is empty.

@erfanium
Copy link
Collaborator

erfanium commented Jul 15, 2021

optional flag only cares about null and undefined values, it doesn't skip empty strings

@sajcics
Copy link
Author

sajcics commented Jul 15, 2021

it will be great if empty string also consider as null and undefined for optional...because it's empty and do not representing anything

@erfanium
Copy link
Collaborator

erfanium commented Jul 15, 2021

You can add a preValidation middleware to delete these keys from object. it's more safe for the validator to be strict

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

No branches or pull requests

2 participants