fix: prioritize registration errors over inline validations in backendValidations

Registration errors from form submission (e.g., "password too similar to
username") were being masked by stale inline validation results. The
backendValidations memo checked state.validations first, which was set
during on-blur field validation with no errors, causing it to never reach
the registrationError branch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Adolfo R. Brandes
2026-03-05 16:20:34 -03:00
committed by Adolfo R. Brandes
parent 0d709d1565
commit 93bd0f24fe
2 changed files with 35 additions and 4 deletions

View File

@@ -322,6 +322,37 @@ describe('RegisterContext', () => {
});
});
it('should prioritize registrationError over validations for backendValidations', () => {
const { result } = renderHook(() => useRegisterContext(), { wrapper });
// Simulate inline validation (on blur) setting validations
act(() => {
result.current.setValidationsSuccess({
validationDecisions: {
password: '',
username: '',
},
});
});
expect(result.current.backendValidations).toEqual({
password: '',
username: '',
});
// Simulate form submission returning a registration error
act(() => {
result.current.setRegistrationError({
errorCode: [{ userMessage: 'validation-error' }],
password: [{ userMessage: 'The password is too similar to the username.' }],
});
});
expect(result.current.backendValidations).toEqual({
password: 'The password is too similar to the username.',
});
});
it('should return null for backendValidations when neither validations nor registrationError exist', () => {
const { result } = renderHook(() => useRegisterContext(), { wrapper });
expect(result.current.backendValidations).toBe(null);

View File

@@ -139,10 +139,6 @@ export const RegisterProvider: FC<RegisterProviderProps> = ({ children }) => {
}, []);
const backendValidations = useMemo(() => {
if (state.validations) {
return state.validations.validationDecisions;
}
if (state.registrationError && Object.keys(state.registrationError).length > 0) {
const fields = Object.keys(state.registrationError).filter(
(fieldName) => !(['errorCode', 'usernameSuggestions'].includes(fieldName)),
@@ -155,6 +151,10 @@ export const RegisterProvider: FC<RegisterProviderProps> = ({ children }) => {
return validationDecisions;
}
if (state.validations) {
return state.validations.validationDecisions;
}
return null;
}, [state.validations, state.registrationError]);