Skip to content

Commit

Permalink
fix: validation to reject british pound as a special character
Browse files Browse the repository at this point in the history
  • Loading branch information
shontzu-deriv committed Sep 13, 2024
1 parent 3bf6c2e commit e1a1e0c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ const ResetTradingPassword = ({
min_number: 8,
max_number: max_length,
});
} else if (platform === CFD_PLATFORMS.MT5 && !validMT5Password(values.password)) {
errors.password = getErrorMessages().special_characters();
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
} else if (platform === CFD_PLATFORMS.MT5 && !validMT5Password(values.password)) {
errors.password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
}

return errors;
Expand Down
8 changes: 1 addition & 7 deletions packages/cfd/src/Containers/cfd-password-change.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ const CFDPasswordChange = observer(
if (response.error.code === 'PasswordError')
actions.setFieldError('old_password', response.error.message);
if (response.error.code === 'InputValidationFailed')
actions.setFieldError(
'new_password',
// Localize is employed to convert the customized error message since the backend error lacks clarity.
localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
)
);
actions.setFieldError('new_password', getErrorMessages().special_characters());
}

if (!response.error) {
Expand Down
6 changes: 4 additions & 2 deletions packages/cfd/src/Containers/cfd-password-manager-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@deriv/components';
import { useDevice } from '@deriv-com/ui';
import { localize, Localize } from '@deriv/translations';
import { getCFDPlatformLabel } from '@deriv/shared';
import { getCFDPlatformLabel, getErrorMessages, validMT5Password } from '@deriv/shared';
import { FormikErrors } from 'formik';
import CFDStore from '../Stores/Modules/CFD/cfd-store';
import TradingPasswordManager from './trading-password-manager';
Expand Down Expand Up @@ -169,7 +169,9 @@ const CFDPasswordManagerTabContent = ({
errors.new_password = localize('This field is required');
}

if (validatePassword(values.new_password)) errors.new_password = validatePassword(values.new_password);
if (platform === CFD_PLATFORMS.MT5 && !validMT5Password(values.new_password)) {
errors.new_password = getErrorMessages().special_characters();
} else if (validatePassword(values.new_password)) errors.new_password = validatePassword(values.new_password);

return errors;
};
Expand Down
8 changes: 3 additions & 5 deletions packages/cfd/src/Containers/cfd-password-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,14 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
min_number: 8,
max_number: max_length,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
} else if (
platform === CFD_PLATFORMS.MT5 &&
should_set_trading_password &&
!validMT5Password(values.password)
) {
errors.password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
errors.password = getErrorMessages().special_characters();
} else if (!validPassword(values.password)) {
getErrorMessages().password();
}
if (values.password?.toLowerCase() === email.toLowerCase()) {
errors.password = localize('Your password cannot be the same as your email address.');
Expand Down
8 changes: 3 additions & 5 deletions packages/cfd/src/Containers/cfd-reset-password-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ const CFDResetPasswordModal = observer(({ platform }: TCFDResetPasswordModal) =>
min_number: 8,
max_number: max_length,
});
} else if (!validPassword(values.new_password)) {
errors.new_password = getErrorMessages().password();
} else if (platform !== CFD_PLATFORMS.DXTRADE && !validMT5Password(values.new_password)) {
errors.new_password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
errors.new_password = getErrorMessages().special_characters();
} else if (!validPassword(values.new_password)) {
errors.new_password = getErrorMessages().special_characters();
}
if (values.new_password.toLowerCase() === email.toLowerCase()) {
errors.new_password = localize('Your password cannot be the same as your email address.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InlineMessage, Modal, Text, PasswordInput, FormSubmitButton } from '@deriv/components';
import { useMT5SVGEligibleToMigrate } from '@deriv/hooks';
import { CFD_PLATFORMS, WS, validLength, validPassword, getErrorMessages } from '@deriv/shared';
import { CFD_PLATFORMS, WS, validLength, validMT5Password, getErrorMessages } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { Localize, localize } from '@deriv/translations';
import React from 'react';
Expand Down Expand Up @@ -58,8 +58,8 @@ const MT5MigrationBackSideContent = observer(() => {
min_number: 8,
max_number: 25,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
} else if (!validMT5Password(values.password)) {
errors.password = getErrorMessages().special_characters();
}
if (values.password?.toLowerCase() === email.toLowerCase()) {
errors.password = localize('Your password cannot be the same as your email address.');
Expand Down
9 changes: 4 additions & 5 deletions packages/cfd/src/Helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
validPassword,
validMT5Password,
mobileOSDetectAsync,
hasBritishPound,
} from '@deriv/shared';
import { localize } from '@deriv/translations';
import { TCFDsPlatformType, TDetailsOfEachMT5Loginid, TMobilePlatforms } from 'Components/props.types';
Expand Down Expand Up @@ -139,12 +140,10 @@ const validatePassword = (password: string): string | undefined => {
min_number: 8,
max_number: 16,
});
} else if (!validPassword(password)) {
return getErrorMessages().password();
} else if (!validMT5Password(password)) {
return localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
return getErrorMessages().special_characters();
} else if (!validPassword(password)) {
return hasBritishPound(password) ? getErrorMessages().special_characters() : getErrorMessages().password();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import React from 'react';
import classNames from 'classnames';
import { Formik, Form, FormikHelpers, FormikErrors } from 'formik';
import { Button, Dialog, PasswordInput, PasswordMeter, Text } from '@deriv/components';
import { redirectToLogin, validPassword, validLength, getErrorMessages, WS, removeActionParam } from '@deriv/shared';
import {
redirectToLogin,
validPassword,
validLength,
getErrorMessages,
WS,
removeActionParam,
validMT5Password,
} from '@deriv/shared';
import { getLanguage, localize, Localize } from '@deriv/translations';
import { observer, useStore } from '@deriv/stores';
import { TSocketError, TSocketRequest, TSocketResponse } from '@deriv/api/types';
Expand Down Expand Up @@ -84,6 +92,8 @@ const ResetPasswordModal = observer(() => {
min_number: 8,
max_number: 25,
});
} else if (!validMT5Password(values.password)) {
errors.password = getErrorMessages().special_characters();
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/Constants/form-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const FORM_ERROR_MESSAGES = {
general: () => localize('Only letters, numbers, space, hyphen, period, and apostrophe are allowed.'),
name: () => localize('Letters, spaces, periods, hyphens, apostrophes only.'),
password: () => localize('Password should have lower and uppercase English letters with numbers.'),
special_characters: () =>
localize('Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'),
po_box: () => localize('P.O. Box is not accepted in address'),
phone: () => localize('Please enter a valid phone number (e.g. +15417541234).'),
postcode: () => localize('Only letters, numbers, space and hyphen are allowed.'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const validName = (value: string) => /^(?!.*\s{2,})(?!\s)[\p{L}\s'.-]{1,5
export const validLength = (value = '', options: TOptions) =>
(options.min ? value.length >= Number(options.min) : true) &&
(options.max ? value.length <= Number(options.max) : true);
export const hasBritishPound = (value: string) => /£/.test(value);
export const validPassword = (value: string) => /^(?=.*[a-z])(?=.*\d)(?=.*[A-Z])[!-~]{8,25}$/.test(value);
export const validEmail = (value: string) => /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/.test(value);
const validBarrier = (value: string) => /^[+-]?\d+\.?\d*$/.test(value);
Expand All @@ -74,7 +75,8 @@ export const hasInvalidCharacters = (target_string: string) => /[^\dX\s]/.test(t
export const isFormattedCardNumber = (target_string: string) =>
/(^\d{4})\s(\d{2}X{2})\s(X{4})\s(\d{4}$)/.test(target_string);
export const validFile = (file: File) => file?.type && /(image|application)\/(jpe?g|pdf|png)$/.test(file?.type);
export const validMT5Password = (value: string) => /^(?=.*[!@#$%^&*()+\-=[\]{};':"|,.<>/?_~])[ -~]{8,16}$/.test(value);
export const validMT5Password = (value: string) =>
/^(?=.*[!@#$%^&*()+\-=[\]{};':"|,.<>/?_~])[ -~]{8,16}$/.test(value) && !hasBritishPound(value);

let pre_build_dvrs: TInitPreBuildDVRs, form_error_messages: TFormErrorMessagesTypes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TFormErrorMessagesTypes = Record<
| 'general'
| 'name'
| 'password'
| 'special_characters'
| 'po_box'
| 'phone'
| 'postcode'
Expand Down

0 comments on commit e1a1e0c

Please sign in to comment.