diff --git a/src/register/data/tests/utils.test.js b/src/register/data/tests/utils.test.js new file mode 100644 index 0000000000..56ef582ec8 --- /dev/null +++ b/src/register/data/tests/utils.test.js @@ -0,0 +1,77 @@ +import { isFormValid } from '../utils'; + +describe('Payload validation', () => { + let formatMessage; + let configurableFormFields; + let fieldDescriptions; + + beforeEach(() => { + formatMessage = jest.fn(msg => msg); + configurableFormFields = { + confirm_email: true, + }; + fieldDescriptions = {}; + }); + + test('validates name field correctly', () => { + const payload = { name: ' ' }; + const errors = {}; + const { isValid, fieldErrors } = isFormValid( + payload, + errors, + configurableFormFields, + fieldDescriptions, + formatMessage); + + expect(fieldErrors.name).toBeDefined(); + expect(isValid).toBe(false); + }); + + test('validates email field correctly', () => { + const payload = { email: 'invalid-email' }; + const errors = {}; + const { isValid, fieldErrors } = isFormValid( + payload, errors, configurableFormFields, fieldDescriptions, formatMessage); + + expect(fieldErrors.email).toBeDefined(); + expect(isValid).toBe(false); + }); + + test('validates username field correctly', () => { + const payload = { username: 'invalid username' }; + const errors = {}; + const { isValid, fieldErrors } = isFormValid( + payload, errors, configurableFormFields, fieldDescriptions, formatMessage); + + expect(fieldErrors.username).toBeDefined(); + expect(isValid).toBe(false); + }); + + test('validates password field correctly', () => { + const payload = { password: 'short' }; + const errors = {}; + const { isValid, fieldErrors } = isFormValid( + payload, errors, configurableFormFields, fieldDescriptions, formatMessage); + + expect(fieldErrors.password).toBeDefined(); + expect(isValid).toBe(false); + }); + + test('validates multiple fields correctly', () => { + const payload = { + name: 'InvalidName!', + email: 'invalid-email', + username: 'invalid username', + password: 'short', + }; + const errors = {}; + const { isValid, fieldErrors } = isFormValid( + payload, errors, configurableFormFields, fieldDescriptions, formatMessage); + + expect(fieldErrors.name).toBeDefined(); + expect(fieldErrors.email).toBeDefined(); + expect(fieldErrors.username).toBeDefined(); + expect(fieldErrors.password).toBeDefined(); + expect(isValid).toBe(false); + }); +}); diff --git a/src/register/data/utils.js b/src/register/data/utils.js index b0cff129a3..747f2f2c30 100644 --- a/src/register/data/utils.js +++ b/src/register/data/utils.js @@ -43,30 +43,39 @@ export const isFormValid = ( Object.keys(payload).forEach(key => { switch (key) { case 'name': - fieldErrors.name = validateName(payload.name, formatMessage); + if (!fieldErrors.name) { + fieldErrors.name = validateName(payload.name, formatMessage); + } if (fieldErrors.name) { isValid = false; } break; case 'email': { - const { - fieldError, confirmEmailError, suggestion, - } = validateEmail(payload.email, configurableFormFields?.confirm_email, formatMessage); - if (fieldError) { - fieldErrors.email = fieldError; - isValid = false; - } - if (confirmEmailError) { - fieldErrors.confirm_email = confirmEmailError; - isValid = false; + if (!fieldErrors.email) { + const { + fieldError, confirmEmailError, suggestion, + } = validateEmail(payload.email, configurableFormFields?.confirm_email, formatMessage); + if (fieldError) { + fieldErrors.email = fieldError; + isValid = false; + } + if (confirmEmailError) { + fieldErrors.confirm_email = confirmEmailError; + isValid = false; + } + emailSuggestion = suggestion; } - emailSuggestion = suggestion; + if (fieldErrors.email) { isValid = false; } break; } case 'username': - fieldErrors.username = validateUsername(payload.username, formatMessage); + if (!fieldErrors.username) { + fieldErrors.username = validateUsername(payload.username, formatMessage); + } if (fieldErrors.username) { isValid = false; } break; case 'password': - fieldErrors.password = validatePasswordField(payload.password, formatMessage); + if (!fieldErrors.password) { + fieldErrors.password = validatePasswordField(payload.password, formatMessage); + } if (fieldErrors.password) { isValid = false; } break; default: