forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ameerul /P2PS-1564 Migrate validations.js file to typescript (deriv-c…
…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
1 parent
1bc85d2
commit 9bec43c
Showing
4 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |