Skip to content

Conversation

@elpete
Copy link
Contributor

@elpete elpete commented Feb 16, 2022

This validator is useful when a field is not required but if it exists it cannot be empty.
This is needed since type validators ignore empty values as valid, but an empty string is not a date!

Here's why I added it:

{
    "startDate": {
        "required": false,
        "type": "date"
    }
}

These validation constraints are typical in our search endpoint — no keys are needed, but if it exists it needs to be a date. Unfortunately, required counts empty strings as valid and type ignores empty strings as "not required". Then my application is dealing with an empty string trying to use it as a date.

With this new constraint, I would add it in like so:

{
    "startDate": {
        "required": false,
        "empty": false,
        "type": "date"
    }
}

It still isn't required, but the value must be a non-empty value and it must be parseable as a date.

This validator is useful when a field is not required but if it exists it cannot be empty.
This is needed since type validators ignore empty values as valid, but an empty string is not a date!
@elpete elpete requested a review from lmajano February 16, 2022 20:59
@lmajano lmajano merged commit 5f42393 into development Jun 27, 2022
@lmajano lmajano deleted the empty branch June 27, 2022 21:44
@lmajano
Copy link
Contributor

lmajano commented Jun 27, 2022

@elpete can you please send me a PR for the docs on this please?

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.

3 participants