diff --git a/packages/p2p/src/stores/my-ads-store.js b/packages/p2p/src/stores/my-ads-store.js index cdc2b721d127..071d503d5bde 100644 --- a/packages/p2p/src/stores/my-ads-store.js +++ b/packages/p2p/src/stores/my-ads-store.js @@ -602,7 +602,7 @@ export default class MyAdsStore extends BaseStore { : true, v => floating_rate_store.rate_type === ad_type.FLOAT - ? rangeValidator(parseFloat(v), floating_rate_store.float_rate_offset_limit) + ? rangeValidator(parseFloat(v), parseFloat(floating_rate_store.float_rate_offset_limit)) : true, ], }; @@ -749,7 +749,7 @@ export default class MyAdsStore extends BaseStore { : true, v => this.required_ad_type === ad_type.FLOAT - ? rangeValidator(v, parseFloat(floating_rate_store.float_rate_offset_limit)) + ? rangeValidator(parseFloat(v), parseFloat(floating_rate_store.float_rate_offset_limit)) : true, ], }; diff --git a/packages/p2p/src/utils/__tests__/validations.spec.ts b/packages/p2p/src/utils/__tests__/validations.spec.ts new file mode 100644 index 000000000000..2d3e021e5964 --- /dev/null +++ b/packages/p2p/src/utils/__tests__/validations.spec.ts @@ -0,0 +1,78 @@ +import { + decimalValidator, + lengthValidator, + textValidator, + rangeValidator, + floatingPointValidator, +} from 'Utils/validations'; + +describe('decimalValidator', () => { + it('should return true if the string is a valid decimal number', () => { + expect(decimalValidator('123')).toBeTruthy(); + expect(decimalValidator('123.456')).toBeTruthy(); + }); + it('should return false if the string is not a valid decimal number', () => { + expect(decimalValidator('123.')).toBeFalsy(); + expect(decimalValidator('123.456.789')).toBeFalsy(); + expect(decimalValidator('123.456.')).toBeFalsy(); + expect(decimalValidator('123.456.789')).toBeFalsy(); + }); +}); + +describe('lengthValidator', () => { + it('should return true if the string is between 1 and 300 characters', () => { + expect(lengthValidator('a')).toBeTruthy(); + expect(lengthValidator('a'.repeat(150))).toBeTruthy(); + expect(lengthValidator('a'.repeat(300))).toBeTruthy(); + }); + it('should return false if the string is not between 1 and 300 characters', () => { + expect(lengthValidator('')).toBeFalsy(); + expect(lengthValidator('a'.repeat(301))).toBeFalsy(); + }); +}); + +describe('textValidator', () => { + it('should return true if the string contains only letters, numbers, spaces, and certain punctuation marks', () => { + expect(textValidator('test')).toBeTruthy(); + expect(textValidator('test123')).toBeTruthy(); + expect(textValidator('test 123')).toBeTruthy(); + expect(textValidator('test 123 .,:;()@#+/-')).toBeTruthy(); + }); + it('should return false if the string contains other characters', () => { + expect(textValidator('test 123 .,:;()@#+/-!')).toBeFalsy(); + }); +}); + +describe('rangeValidator', () => { + it('should return true if the value is within the set range', () => { + expect(rangeValidator(1, 1)).toBeTruthy(); + expect(rangeValidator(1, 2)).toBeTruthy(); + expect(rangeValidator(1, 3)).toBeTruthy(); + expect(rangeValidator(-1, 1)).toBeTruthy(); + expect(rangeValidator(-1, 2)).toBeTruthy(); + expect(rangeValidator(-1, 3)).toBeTruthy(); + }); + it('should return false if the value is not within the set range', () => { + expect(rangeValidator(2, 1)).toBeFalsy(); + expect(rangeValidator(3, 2)).toBeFalsy(); + expect(rangeValidator(4, 3)).toBeFalsy(); + expect(rangeValidator(-2, 1)).toBeFalsy(); + expect(rangeValidator(-3, 2)).toBeFalsy(); + expect(rangeValidator(-4, 3)).toBeFalsy(); + }); +}); + +describe('floatingPointValidator', () => { + it('should return true if the value is a valid floating-point integer', () => { + expect(floatingPointValidator('1')).toBeTruthy(); + expect(floatingPointValidator('1.2')).toBeTruthy(); + }); + it('should return false if the value is not a valid floating-point integer', () => { + expect(floatingPointValidator('1.')).toBeFalsy(); + expect(floatingPointValidator('1.2.3')).toBeFalsy(); + expect(floatingPointValidator('1.2.')).toBeFalsy(); + expect(floatingPointValidator('a')).toBeFalsy(); + expect(floatingPointValidator('1a')).toBeFalsy(); + expect(floatingPointValidator('1.a')).toBeFalsy(); + }); +}); diff --git a/packages/p2p/src/utils/validations.js b/packages/p2p/src/utils/validations.js deleted file mode 100644 index 34bb405ccbfb..000000000000 --- a/packages/p2p/src/utils/validations.js +++ /dev/null @@ -1,12 +0,0 @@ -export const decimalValidator = v => /^(\d+\.)?\d+$/.test(v); - -export const lengthValidator = v => v.length >= 1 && v.length <= 300; - -export const textValidator = v => /^[\p{L}\p{Nd}\s'.,:;()@#+/-]*$/u.test(v); - -// Validates if the given value falls within the set range and returns a boolean -export const rangeValidator = (input, limit) => input >= limit * -1 && input <= limit; - -// validates floating-point integers in input box that do not contain scientific notation (e, E, -, +) such as 12.2e+2 or 12.2e-2 and no negative numbers -export const floatingPointValidator = v => - ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', '.'].includes(v) || /^[0-9]*[.]?[0-9]+$(?:[eE\-+]*$)/.test(v); diff --git a/packages/p2p/src/utils/validations.ts b/packages/p2p/src/utils/validations.ts new file mode 100644 index 000000000000..ae921f232f69 --- /dev/null +++ b/packages/p2p/src/utils/validations.ts @@ -0,0 +1,42 @@ +/** + * Validates that a string represents a decimal number. + * It checks if the string contains only digits and at most one decimal point. + * + * @param {String} value - The string to validate as a decimal number. + * @returns {boolean} A boolean indicating the string is a valid decimal number. + */ +export const decimalValidator = (value: string): boolean => /^(\d+\.)?\d+$/.test(value); + +/** + * Validates the length of the text is between 1 and 300 characters. + * + * @param {String} value - The string to validate length. + * @returns {boolean} A boolean indicating the length of the text is valid. + */ +export const lengthValidator = (value: string): boolean => value.length >= 1 && value.length <= 300; + +/** + * Validates whether a string contains only letters, numbers, spaces, and certain punctuation marks. + * @param {String} value - The string to validate text. + * @returns {boolean} A boolean indicating the text is valid. + */ +export const textValidator = (value: string): boolean => /^[\p{L}\p{Nd}\s'.,:;()@#+/-]*$/u.test(value); + +/** + * Validates if the given value falls within the set range and returns a boolean. + * + * @param {Number} input - The value to validate + * @param {Number} limit - The limit to validate against. + * @returns {boolean} A boolean indicating if the value is within the set range. + */ +export const rangeValidator = (input: number, limit: number): boolean => input >= limit * -1 && input <= limit; + +/** + * Validates floating-point integers in input box and checks if the string contains only + * digits and at most one decimal point. + * + * @param {String} value - The value to validate as a floating-point integer. + * @returns {boolean} A boolean indicating if the value is a valid floating-point integer. + */ +export const floatingPointValidator = (value: string): boolean => + ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', '.'].includes(value) || /^\d*\.?\d+$/.test(value);