Skip to content

Required properties with empty strings #1002

Answered by radekmie
mdesousa asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @mdesousa. The thing is, that an empty string is a valid string. It's up to you to decide what should the schema check for. I'd suggest one of the following, in the order of my personal preference:

  1. Use pattern or format to specify the... Well, pattern or format of the string.
  2. Use minLength: N to specify the minimum input length.
  3. Create a custom keyword like isNotEmpty.

Please note that all of the above require changing your schema. That's not necessarily bad, as one, this can be (semi-)automated, and clearly defines your intention.

Also, remember that there's no trimming by default, and you may want to use a custom keyword for that as well. Luckily, ajv-keywords got you covered with …

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@radekmie
Comment options

@Krypt0r7
Comment options

@radekmie
Comment options

@mdesousa
Comment options

@radekmie
Comment options

Answer selected by radekmie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants