Skip to content

Commit

Permalink
Ameerul /P2PS-1564 Migrate validations.js file to typescript (deriv-c…
Browse files Browse the repository at this point in the history
…om#10047)

* chore: migrated validations.js file to typescript, added test cases

* chore: added types for @params in documentation

* chore: added types for @return in documentation

* chore: fixed sonarcloud issue for regex in floatingPointValidator

* chore: annotated types
  • Loading branch information
ameerul-deriv authored Dec 5, 2023
1 parent 1bc85d2 commit 9bec43c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/p2p/src/stores/my-ads-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
};
Expand Down Expand Up @@ -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,
],
};
Expand Down
78 changes: 78 additions & 0 deletions packages/p2p/src/utils/__tests__/validations.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
12 changes: 0 additions & 12 deletions packages/p2p/src/utils/validations.js

This file was deleted.

42 changes: 42 additions & 0 deletions packages/p2p/src/utils/validations.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 9bec43c

Please sign in to comment.