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

Empty string as acceptable value on non-optional field? #64

Closed
noahadler opened this issue Feb 11, 2014 · 2 comments
Closed

Empty string as acceptable value on non-optional field? #64

noahadler opened this issue Feb 11, 2014 · 2 comments

Comments

@noahadler
Copy link

What is the correct way to allow an empty string ('') to validate? I haven't marked this field as optional, but I set min to 0. I'd expect it to reject my insert if I passed null, undefined, or omitted the field entirely, but it seems to also reject an empty string. Is this by design? For certain applications, might it make sense to allow empty string as an acceptable value? I'm willing to try to work around this, but wanted to be sure it's a design decision that I can learn, and not a bug. Thanks!

@aldeed
Copy link
Collaborator

aldeed commented Feb 11, 2014

Yes, an empty string fails required validation by design. If there are enough complaints, this could be changed, but I think it generally makes sense. To make it allow an empty string but not null or undefined, you could use optional: true along with a custom function similar to this:

function () {
  if (!this.operator && this.value == null) {
    return "required";
  }
}

@green-coder
Copy link

I ran into the same corner case, where I have a non-optional string which is allowed to be empty.

Polite complain +1.

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

3 participants