-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from alexlipovka/informative-field-validation…
…-error-display Display more relevant validation errors on BaseQuestionnaireResponseForm
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/components/BaseQuestionnaireResponseForm/__tests__/fieldErrorMessage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ControllerFieldState, ControllerRenderProps, FieldValues } from 'react-hook-form'; | ||
import { describe } from 'vitest'; | ||
|
||
import { getFieldErrorMessage } from 'src/components/BaseQuestionnaireResponseForm/utils'; | ||
|
||
const FIELD_STATE_MESSAGE_MAP = [ | ||
{ | ||
fieldName: 'first-name.0.value.string', | ||
fieldValue: 'NotSoLongReallyName', | ||
fieldText: 'First name', | ||
errorMessage: 'first-name[0].value.string must be at most 30 characters', | ||
errorType: 'max', | ||
expectedResult: 'First name must be at most 30 characters', | ||
}, | ||
{ | ||
fieldName: 'first-name.0.value.string', | ||
fieldValue: 'Basic - 1', | ||
fieldText: 'First name', | ||
errorMessage: 'first-name[0].value.string is a required field', | ||
errorType: 'required', | ||
expectedResult: 'First name is a required field', | ||
}, | ||
]; | ||
|
||
describe('Field error message should be relevant and human readable', () => { | ||
test.each(FIELD_STATE_MESSAGE_MAP)( | ||
'getFieldErrorMessage returns correctly transformed message', | ||
(fieldStateMessage) => { | ||
const field: ControllerRenderProps<FieldValues, any> = { | ||
name: fieldStateMessage.fieldName, | ||
value: fieldStateMessage.fieldValue, | ||
onChange: () => { | ||
return void 0; | ||
}, | ||
onBlur: () => { | ||
return void 0; | ||
}, | ||
ref: () => { | ||
return void 0; | ||
}, | ||
}; | ||
|
||
const fieldState: ControllerFieldState = { | ||
error: { | ||
message: fieldStateMessage.errorMessage, | ||
type: fieldStateMessage.errorType, | ||
}, | ||
invalid: true, | ||
isTouched: true, | ||
isDirty: true, | ||
}; | ||
|
||
const fieldText = fieldStateMessage.fieldText; | ||
|
||
const invalidFieldMessage = getFieldErrorMessage(field, fieldState, fieldText); | ||
|
||
expect(invalidFieldMessage).toBe(fieldStateMessage.expectedResult); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ControllerFieldState, ControllerRenderProps, FieldValues } from 'react-hook-form'; | ||
|
||
export function getFieldErrorMessage( | ||
field: ControllerRenderProps<FieldValues, any>, | ||
fieldState: ControllerFieldState, | ||
text?: string, | ||
) { | ||
if (!fieldState || !fieldState.invalid) { | ||
return undefined; | ||
} | ||
|
||
if (!fieldState.error || !fieldState.error.message) { | ||
return undefined; | ||
} | ||
// replace [0], [1] with .0, .1 to match field.name | ||
const errorMessageWithInternalFieldName = fieldState.error.message.replace(/\[(\d+)\]/g, '.$1'); | ||
|
||
const errorMessageWithHumanReadableFieldName = errorMessageWithInternalFieldName.replace(field.name, text ?? ''); | ||
|
||
return errorMessageWithHumanReadableFieldName; | ||
} |