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(mrf be val): skip validation for unchanged mrf responses #7894

Merged

Conversation

kevin9foong
Copy link
Contributor

@kevin9foong kevin9foong commented Nov 15, 2024

Problem

During MRF BE Validation, false positives occur for verifiable fields (due to signature being marked as invalid) and date fields with disallow past dates validation.

Details

  1. Step 1 of MRF can be submitted on 1st jan 2024, a respondent for step 1 can select the current date 1st jan 2024 which is valid and passes the disallow past dates validation.
  2. Step 2 is then submitted on 3rd jan 2024 and the respondent leaves the date field value from step 1 unchanged. This fails the disallow past dates validation since the step 2 submission date of 3rd jan 2024 is after 1st jan 2024.

This edge case is considered acceptable and is similar to the current (as of this PR) frontend validation behavior. For example, if the admin tries to submit the form at step 2 where the date field with disallow past dates validation is editable, they would experience a frontend validation failure.

Closes FRM-1909

Solution

During field validation, there is a check if validation is required.
An additional check for if the field response is changed is added. In the event the field is unchanged, then the validation for the field is skipped. This is based on the idea where since a response has been validated in the previous step and is unchanged, it does not need to be re-validated in the subsequent step. This resolves the false positive issue and replicates the same frontend validation behavior described above.

Implementation
A new function checkIsResponseChangedV3 is added to a file field-validation.utils.ts which allows for export without having to explicitly export a checkIsResponseChangedV3ForTest if added to the index.ts file. This function is used to determine if validation should be skipped.
This function is exported and unit tested to ensure it correctly checks for changes for the various field types.

Breaking Changes

  • No - this PR is backwards compatible

@datadog-opengovsg
Copy link

datadog-opengovsg bot commented Nov 15, 2024

Datadog Report

Branch report: fix/remove-validation-for-unchanged-mrf-responses
Commit report: 10dd69a
Test service: formsg

✅ 0 Failed, 1285 Passed, 1 Skipped, 2m 16.27s Total duration (4m 30.22s time saved)

Copy link

linear bot commented Nov 15, 2024

@kevin9foong kevin9foong requested a review from KenLSM November 15, 2024 09:22
@kevin9foong kevin9foong self-assigned this Nov 15, 2024
@kevin9foong kevin9foong changed the title Fix/remove validation for unchanged mrf responses fix: remove validation for unchanged mrf responses Nov 15, 2024
@kevin9foong kevin9foong changed the title fix: remove validation for unchanged mrf responses fix(mrf be val): remove validation for unchanged mrf responses Nov 15, 2024
@@ -191,7 +191,9 @@ const pastOnlyValidatorV3: ResponseValidator<DateResponseV3> = (response) => {
const answerDate = createMomentFromDateStringV3(answer)

return answerDate.isAfter(todayMax)
? left(`DateValidatorV3:\t answer does not pass date logic validation`)
? left(
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 changes in this file are added to make the logs more specific - not related to the skipping of validation

@@ -1052,131 +1052,4 @@ describe('Email field validation V3', () => {
new ValidateFieldError('Invalid answer submitted'),
)
})

Copy link
Contributor Author

@kevin9foong kevin9foong Nov 15, 2024

Choose a reason for hiding this comment

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

These TC are removed as they are migrated to the test for checkIsResponseChangedV3 instead, avoiding the need to spy and check if the spied function is invoked

: null
return prevResponseValue !== responseValue
case BasicField.Checkbox:
if (prevResponse.fieldType !== response.fieldType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is required due to TS throwing an error as it fails to infer the type of prevResponse is same as response without it, despite being checked in L19 above.

@kevin9foong kevin9foong force-pushed the fix/remove-validation-for-unchanged-mrf-responses branch from 6ef3ce7 to 7339522 Compare November 15, 2024 10:09
@kevin9foong kevin9foong changed the title fix(mrf be val): remove validation for unchanged mrf responses fix(mrf be val): skip validation for unchanged mrf responses Nov 15, 2024
Copy link
Contributor

@KenLSM KenLSM left a comment

Choose a reason for hiding this comment

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

LGTM!

@kevin9foong kevin9foong merged commit d5a4bae into develop Nov 18, 2024
15 checks passed
@kevin9foong kevin9foong deleted the fix/remove-validation-for-unchanged-mrf-responses branch November 18, 2024 09:17
@kevin9foong kevin9foong mentioned this pull request Nov 19, 2024
14 tasks
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.

2 participants