Skip to content

Fix numeric validator #1382

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

Merged

Conversation

jspannu919
Copy link
Contributor

Currently the numeric validators (min or max) doesn't shows any validation error if the value in the field is zero.

Scenario:
If a field is added with min validation with threshold as 3, the validator will show error for values like 1,2,-1 etc but nothing for 0.

@vercel
Copy link

vercel bot commented May 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-forms ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 26, 2023 7:35am

@DataDrivenFormsBot
Copy link

No new version will be released. Current: v3.20.8 [DataDrivenFormsBot]

@codecov
Copy link

codecov bot commented May 20, 2023

Codecov Report

Merging #1382 (a69abb6) into master (7dedcb4) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1382   +/-   ##
=======================================
  Coverage   95.09%   95.09%           
=======================================
  Files         210      210           
  Lines        3647     3647           
  Branches     1271     1271           
=======================================
  Hits         3468     3468           
  Misses        179      179           
Impacted Files Coverage Δ
...orm-renderer/src/validators/validator-functions.js 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@Hyperkid123 Hyperkid123 left a comment

Choose a reason for hiding this comment

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

@jspannu919 Thanks for the contribution. I have one small suggestion.

@@ -90,7 +90,7 @@ export const numericality = memoize(
lessOrEqual = selectNum(lessOrEqual, lessThanOrEqualTo);

return prepare((value) => {
if (!value) {
if (value == null) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should cover all "falsy" values

Suggested change
if (value == null) {
if (typeof value !== 'number' && typeof value !== 'string') {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The next block to this is checking if the value is a valid number or not, handling these here will lead to no proper message being displayed.

Copy link
Member

@Hyperkid123 Hyperkid123 May 23, 2023

Choose a reason for hiding this comment

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

You are correct. My point is that we also have to consider undefined in addition to null. I guess we can let the rest of the types through this check. Oh and also, can you make sure to use the strict equality ===?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have not added strict equality intentionally, to cover "null" as well as "undefined" case.

Copy link
Member

Choose a reason for hiding this comment

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

These strings will be handled by the isNumber check below as you pointed out. We like to use the strict equality operator whenever possible to avoid unexpected "truthy" results. It's a matter of convention.

We can go ahead and merge/release the PR afterward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry if I was not able to explain it properly. I didn't meant to cover the null and undefined "strings" but the values, so basically to cover (value === null || value === undefined) with just ( value == null ) as it will return true for undefined also. Let me know if you think we should explicitly check for undefined with strict equality, I will make the change.

Copy link
Member

Choose a reason for hiding this comment

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

@jspannu919 yes, please. We try to use strict equality as much as possible to avoid some unintentional condition outcomes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@rvsia rvsia added bug Something isn't working renderer React form renderer PR labels May 26, 2023
@Hyperkid123 Hyperkid123 merged commit 614265a into data-driven-forms:master May 26, 2023
@rvsia rvsia added the released label May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released renderer React form renderer PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants