Skip to content

Commit

Permalink
fix: frontend validation on email field (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque authored Jun 12, 2024
1 parent 8efb225 commit 99bca1b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 14 deletions.
77 changes: 77 additions & 0 deletions src/register/data/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
37 changes: 23 additions & 14 deletions src/register/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 99bca1b

Please sign in to comment.