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

fix/16611: Trim value before validation in Form Component #16861

Merged
merged 4 commits into from
Apr 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@ class Form extends React.Component {
* @returns {Object} - An object containing the errors for each inputID, e.g. {inputID1: error1, inputID2: error2}
*/
validate(values) {
const trimmedValues = {};
_.each(values, (inputValue, inputID) => _.isString(inputValue) && (trimmedValues[inputID] = inputValue.trim()));
Copy link
Member

@rushatgabhane rushatgabhane Apr 2, 2023

Choose a reason for hiding this comment

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

won't this crash the app when we're trimming an empty string?

emptyString = ''
isString(emptyString) is true
emptyString.trim() ---> crash 💥

This comment was marked as outdated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh wait me, I need to check again

Copy link
Contributor Author

@tienifr tienifr Apr 2, 2023

Choose a reason for hiding this comment

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

@rushatgabhane
I don't see that emptyString.trim() ---> crash 💥, emptyString.trim() = ""
The crash app can be caused from some boolean fields
In the first commit, I added the condition _.isString(inputValue) to avoid the crash
And I just updated _.isString(inputValue) into !_.isEmpty(inputValue) So it also resolved this problem because _.isEmpty(boolean) === true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@rushatgabhane rushatgabhane Apr 3, 2023

Choose a reason for hiding this comment

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

@tienifr ah i see there's a misunderstanding

here is what i mean
allValues = [...trimmedStringValues, ...nonTrimmedNonStringValues]

vs what we're doing right now -
allValues = [...trimmedStringValues]

Copy link
Member

@rushatgabhane rushatgabhane Apr 3, 2023

Choose a reason for hiding this comment

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

I'm not suggesting that we should trim non-string values.

We're completely dropping non-string values. I'm suggesting that we don't drop them, pass them as they are

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rushatgabhane Oh, my bad 😄 Just updated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rushatgabhane Could you help to take a look at this PR? It is given an approval

FormActions.setErrors(this.props.formID, null);
FormActions.setErrorFields(this.props.formID, null);

// Run any validations passed as a prop
const validationErrors = this.props.validate(values);
const validationErrors = this.props.validate(trimmedValues);
Copy link
Member

Choose a reason for hiding this comment

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

Will the errors for non-string values will still be validated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rushatgabhane I am checking if it is a string value, it will be trimmed. So that non-string values will not be trimmed but it still is validated as before.
We can replace _.isString(inputValue) into !_.isEmpty(inputValue) then it will check for both string and non-string values
But I don't see any values are non-string in Form

Copy link
Member

Choose a reason for hiding this comment

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

But I don't see any values are non-string in Form

agree with you. but then there's a reason you're checking if the value is string before trimming.

Form is a generic component so I think we should handle non string values as well.


// Validate the input for html tags. It should supercede any other error
_.each(values, (inputValue, inputID) => {
_.each(trimmedValues, (inputValue, inputID) => {
// Return early if there is no value OR the value is not a string OR there are no HTML characters
if (!inputValue || !_.isString(inputValue) || inputValue.search(CONST.VALIDATE_FOR_HTML_TAG_REGEX) === -1) {
return;
Expand Down