-
Notifications
You must be signed in to change notification settings - Fork 87
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
feat: add range validation to number field #6575
Conversation
Hi @LeonardYam! Thanks for the contribution. I'll be working with you to get this PR merged in as soon as possible 😄 While I review this PR, could I trouble you to open another PR to clean up the old virtuals in the Thanks again! |
a3db64a
to
849e71c
Compare
Hi @justynoh, I've just rebased the pull request. Let me know if there are any issues! 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! I've briefly reviewed the backend, while our designer is on leave. I'll get to the frontend when our designer is back later this week, and let you know what changes need to be made.
Additionally, we would need your help to write a mongo script to perform a migration of existing form documents from the old to the new schema. You can look in the
|
Hi @justynoh, apologies for the delay! I have gone through the previous code review and applied the suggestions accordingly. Could I double-check on what tests I should perform to verify that the script works as expected? |
Hi @LeonardYam, just a quick update from my end. I'll take a look as soon as I can, but this period has been quite busy for me! I'd seek your understanding to give me some time to review it when I next can, which will likely come next week. Don't worry, we haven't forgotten about your contribution! And apologies for the delay 😅 |
Implement additional validation in EditNumber.tsx to ensure invalid ranges cannot be filled.
1. For selectedValidation, create new enum in the frontend and simplify shared enum values. 2. Rename rangeMinimum and rangeMaximum to customMin and customMax respectively. 3. Rewrite typeof checks to truthiness checks in backend validators. 4. Remove unnecessary '' from selectedLengthValidation
1163b1c
to
1d6652a
Compare
Hi @justynoh, no worries! I've just added the mongodb script, which ended up being very verbose. However to my understanding, there aren't many alternatives since |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is starting to shape up nicely! A number of comments from my end, as well as some updated copy. The migration script also looks good, thanks for writing it up!
The only thing I haven't reviewed is the updated EditNumber.tsx
frontend component. The reason for this is that we have updated screens for your reference. If you could help implement them, then on my next review I'll look at 1) the new number field creation UI and EditNumber.tsx
and 2) the updates based on my comments.
The screens and some other considerations are coming up in my next comment, for your reference.
Hi @justynoh, I've just went through the code review and applied the initial round of fixes. Here's a quick look at the current UI screens: (updated copy of error messages, validation of empty and 0 values etc.) range.mp4Other than the comments above, I also have some questions about the new UI screens. I think the inclusion of a
I'm leaning towards the first option since this would be more consistent with the current UI, as this pattern is already used in date validation for example. On a related note, should we also keep the dropdown for length validation clearable? This way, we would be consistent with the character length validation in short answer and long answer validation. Let me know what would be best! |
Hi Leonard! Thanks for looking at the UI and doing up the new frontend. For your points, I agree with doing (1). We can remove the "None" option and use an empty input to represent no validation instead. As for the dropdown for length validation, it should not be clearable. Since we already only display it when the user selects "Length validation", the user has made a conscious choice to select a validation type. Once they pick one type of length validation (Min/Max/Exact), it doesn't make sense to allow them to remove it at that level. If they want to clear the validation, they should press "X" on the validation type dropdown, not the length validation dropdown. Finally, as a UI nit: we should remove the form labels for "Minimum and/or maximum value" and "Number of characters allowed", above the conditionally rendered inputs. Instead, the spacing between the upper dropdown and the lower inputs should be 0.5rem. Thanks! |
Hi @justynoh, I've just went through the review again and there is only one last part I'm unsure about. Other than that, I've just realized that we actually use the Thanks for looking through the lengthy comments 😅 ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple more nits, and we should be good to go!
As for using the validatorjs library, I think it's up to you. What you've implemented is fine, and I suspect there will be some overhead for handling null
cases when using the library anyway, so it might not really simplify the code that much. I would prefer to just keep it as you've implemented it.
...r-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditNumber/EditNumber.tsx
Outdated
Show resolved
Hide resolved
...r-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditNumber/EditNumber.tsx
Outdated
Show resolved
Hide resolved
...r-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditNumber/EditNumber.tsx
Outdated
Show resolved
Hide resolved
...r-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditNumber/EditNumber.tsx
Outdated
Show resolved
Hide resolved
Hi @justynoh, I've just made the relevant changes. Let me know if there are any final changes needed! On a side note, while I was checking the validity of the back-end validators, I actually had an issue with getting them to run as my |
Hey Leonard, Overall, the code on this PR looks pretty much good to go! Feel free to address the one comment I gave on your question above. To answer your question regarding Now, the last piece of this PR before I can approve it would be writing appropriate manual tests for the feature as well as updating the existing test suite to account for the changes. Concretely, you will definitely have to update the tests in Additionally, I would request your help to write manual tests. Generally, we try and write two types of tests - one set to cover new functionality, and another set for regression. Here is a sample of what we might expect to see in the "Tests" section of the PR:
Feel free to let me know once you've updated the tests, and I'll be happy to add on if required, and then approve and merge the PR. Thanks! |
Hi @justynoh, you were right - I was indeed using storage mode responses 😅, after changing the mode the backend validators managed to fire. I just did a quick update on the backend tests, mainly renaming the test names to include length and updating the schemas. During the update, I came across the test Unfortunately, I've been very busy recently and I would need some time to finish up the PR 😅. Just to confirm, the remaining work would be to:
Am I missing out on any additional tasks? |
For your question regarding validator implementation - I don't think this is intentional. We should implement our validators for this feature in a way that ensures that this is not possible (i.e. if Length, it must have both As for the remaining tasks, yes, those seem like the remaining items to me. Thanks for the continuous work on this @LeonardYam! We're excited to get this merged. |
Hi @justynoh, I took a look at the validators again and I believe that for our backend validators Hence, there should not be any changes needed for the current validators. I've instead deleted the tests that are based on response validation on invalid options and re-written them as mentioned below. For robustness, I've written automated tests for:
I believe the tests which cannot be automated currently involve testing of error messages on Additionally, I've also added placeholder text to selectedLengthValidation. Let me know if there's anything more to add! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! this is great work, thanks so much for the contribution @LeonardYam! We'll merge it in as soon as we can.
Closing and re-opening to get tests to run. |
merged develop into this branch to include CI updates! |
Problem
Closes #378
Solution
Breaking Changes
shared/types/field/numberField.ts
Changes by file
NumberValidationOptions
schema inshared/types/field/numberField.ts
to include range validationfrontend/src/features/admin-form/create/builder-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditNumber/EditNumber.tsx
frontend/src/features/admin-form/create/builder-and-design/utils/fieldCreation.ts
to reflect the updated schemaNumberField
infrontend/src/utils/fieldValidation.ts
src/app/models/field/numberField.ts
src/app/utils/field-validation/validators/numberValidator.ts
Before & After Screenshots
BEFORE:
AFTER:
Tests
Manual tests for range validation:
Range of values allowed
underField restriction
.0
for minimum or maximum value. This should display an error.Regression tests (for
EditNumber.tsx
)Number of characters allowed
underField restriction
.Number of characters
. This should display an error.Concerns
Character Length
orNumber Range
selected but no custom values to be created)Deploy notes
New scripts:
migrate-number-field-schema.js
: This script migratesValidationOptions
toValidationOptions.LengthValidationOptions
for all existing number form fields.