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

consider null as a value #317

Merged
merged 5 commits into from
Mar 1, 2023
Merged

consider null as a value #317

merged 5 commits into from
Mar 1, 2023

Conversation

Freezystem
Copy link
Contributor

@Freezystem Freezystem commented Feb 18, 2023

This PR contains breaking changes and should be reviewed with care.

All fields are considered nullable by default.

It allows a field to be:

  • required and null.
  • optional but not null.

fix #296

@@ -9,7 +9,7 @@ describe("Test rule: any", () => {
it("should give back true anyway", () => {
const check = v.compile({ $$root: true, type: "any" });

expect(check(null)).toEqual([{ type: "required", actual: null, message: "The '' field is required." }]);
expect(check(null)).toEqual(true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not good that this case is changed. As you wrote in the PR name null is a value, so the required rule should accept it. It should be true if nullable: false

Copy link
Contributor Author

@Freezystem Freezystem Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm not sure I'm following you on this point.
In my mind, the case seems good:
Here we consider that nullable is true by default as it's not explicitly set to false.
Given that any accept any type and with $$root modifier any accept any value, it can be anything but undefined which is not a value.
So when any type is used, null should be a valid value.
At the contrary, if nullable=false is set, any could be anything expect null or undefined.

Unless we consider the default value of nullable to false but I'm scared that it will defeat the purpose and implies bigger changes to the test suite.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, sorry I was wrong, I changed the lines in my mind. By the way, I would like to avoid the breaking change....breaking FV generates breaking changes in Moleculer, it means, we can't use it only just in the next version 0.15

To cover similar issue, we add a global option which switch the old/new behaviour. Can we do something similar here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I also had trouble getting my mind around it at first 😄
I'll add to this PR a global setting to be able to switch between old and new behaviour.

Copy link
Contributor Author

@Freezystem Freezystem Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also added some documentation and tests that seems relevant for specific edge cases.

4 cases are tested when considerNullAsAValue option is set to true:

  • field is required and nullable, default value will NOT apply. (default)
  • field is required but NOT nullable, default value will apply if field is null or undefined.
  • field is optional and nullable, default will NOT apply.
  • field is optional but NOT nullable, default value will apply if field is null or undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @icebob, feel free to tell me if you need me to do some other changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sorry for late, I'm checking...

Copy link
Owner

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, thanks!

@icebob icebob merged commit 5fe01c3 into icebob:master Mar 1, 2023
@Freezystem Freezystem deleted the fix/consider-null-a-value branch March 2, 2023 09:39
@Freezystem
Copy link
Contributor Author

Hi @icebob, is it possible to publish a new release that include this feature ?

@icebob
Copy link
Owner

icebob commented Apr 23, 2023

Sorry, I forgot it. I've just released as 1.17.0

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.

Is it possible to allow undefined value validation but return an error for null value?
2 participants