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

Possible bug: Optional enum string fails validation on empty input #283

Closed
giovanni-bertoncelli opened this issue Feb 2, 2022 · 2 comments · Fixed by #284
Closed

Possible bug: Optional enum string fails validation on empty input #283

giovanni-bertoncelli opened this issue Feb 2, 2022 · 2 comments · Fixed by #284

Comments

@giovanni-bertoncelli
Copy link

Description

I do not know if this behaviour is by design or not but when I declare some prop like this one:

const schema = {
      Status: {
        type: 'string',
        uppercase: true,
        enum: ['a', 'b', 'c'],
        optional: true,
        nullable: true
      }
}
validate({Status: ''}, schema)

the validation process fails inferring that the empty value '' is not in the stringEnum, even if I've specified the optional and the nullable features. Is this by design? Or shouldn't be the null and empty string, in this case, considered as part of the enumeration or checked before validating the enum? If so it should be documented or at least using optional or nullable with enum should throw an exception...

@icebob
Copy link
Owner

icebob commented Feb 7, 2022

I think it's a bug, I'm checking it.

@icebob
Copy link
Owner

icebob commented Feb 7, 2022

I've checked the code, and it looks the half is by design and half is (maybe bug or not).

  • the optional: true checks that Status === undefined˙.
  • the nullable: true checks that Status === null
    But the empty string is neither of both. There is an empty: false property that disables accepting empty string, but empty: true has no effect, just skip this check and rule other checks, like the enum.

The solution would be if we add check for empty: true as well, which skips other check if the value is ''.

icebob added a commit that referenced this issue Feb 7, 2022
@icebob icebob linked a pull request Feb 7, 2022 that will close this issue
icebob added a commit that referenced this issue Feb 8, 2022
check `empty: true` in string rule. Fixes #283
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants