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

Null value for boolean attribute is converted to false. How to alter this behavior? #1444

Closed
1 of 3 tasks
RohitRox opened this issue Jun 26, 2023 · 6 comments · Fixed by #1578
Closed
1 of 3 tasks

Comments

@RohitRox
Copy link

RohitRox commented Jun 26, 2023

This is related to validation. Null values sent for attributes defined as boolean are converted to false.

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • [x ] used the search to make sure that a similar issue hasn't already been submit

Current & Expected Behavior

When an attribute is defined as a boolean
For eg:

interface UserCreateRequest {
    preferredDelivery: boolean
}

and in payload/request body, if null is sent
For eg:

{
    "preferredDelivery": null
}

After the request passes through validation, the preferredDelivery's value is observed to be false.
The expected behavior is that the null value should have been preserved.

Possible Solution

I understand in some cases this is okay and desired, but is some cases this behavior is needed. And I couldn't find a way to alter this behavior.
The validator library (https://github.com/validatorjs/validator.js) seem to have { loose: false } option but I could not find a way to use it here.
Any help or guidance on this?

@github-actions
Copy link

Hello there RohitRox 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@WoH
Copy link
Collaborator

WoH commented Jul 22, 2023

Hi there, you can customize the template as well as the route generator (template is simpler and should be sufficient).

However, we should also discuss these groups of coercions and if we want to customize them (i.e. via config or hooks).

However, as is, this is intended behavior, therefore I'll tag this properly.

@jchadwick
Copy link

This issue is more obvious (and more obviously a bug vs intended behavior) when the boolean property explicitly allows nulls. e.g.

interface UserCreateRequest {
    preferredDelivery: boolean | null
}

This also improperly converts explicitly null values to false

@WoH
Copy link
Collaborator

WoH commented Mar 13, 2024

This issue is more obvious (and more obviously a bug vs intended behavior) when the boolean property explicitly allows nulls. e.g.

interface UserCreateRequest {
    preferredDelivery: boolean | null
}

This also improperly converts explicitly null values to false

Try null | boolean

@jchadwick
Copy link

WOW, WAIT... WHAT!?

Yeah, that works. But why?? And can we make the more "standard" boolean | null work as well?

@WoH
Copy link
Collaborator

WoH commented Mar 13, 2024

Validation is from left to right. Thus, so is coercion.

So:

  • validateUnion([boolean, null], null)
    -> validate Boolean schema
    -> coerce null to false
    -> pass with false

vs.

  • validateUnion([null, boolean], null)
    -> validateLiteral(null, null)
    -> pass with null

We can make it work, by rewriting the entire metadata layer to output json schemas for ajv or basically reimplement ajv ourselves.
I dont have plans to do either.

@WoH WoH closed this as completed in #1578 Mar 24, 2024
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.

3 participants