mirror of
https://github.com/ansible/awx.git
synced 2026-01-15 03:40:42 -03:30
add number validator support for negative numbers, large numbers
This commit is contained in:
parent
a9c01e891f
commit
9b5e59f045
@ -79,7 +79,11 @@ export function integer(i18n) {
|
||||
export function number(i18n) {
|
||||
return value => {
|
||||
const str = String(value);
|
||||
if (/^[0-9]*(\.[0-9]*)?$/.test(str)) {
|
||||
if (/^-?[0-9]*(\.[0-9]*)?$/.test(str)) {
|
||||
return undefined;
|
||||
}
|
||||
// large number scientific notation (e.g. '1e+21')
|
||||
if (/^-?[0-9]*e[+-][0-9]*$/.test(str)) {
|
||||
return undefined;
|
||||
}
|
||||
return i18n._(t`This field must be a number`);
|
||||
|
||||
@ -121,10 +121,19 @@ describe('validators', () => {
|
||||
expect(number(i18n)('13')).toBeUndefined();
|
||||
});
|
||||
|
||||
test('number should accept negative number', () => {
|
||||
expect(number(i18n)(-14)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('number should accept decimal/float', () => {
|
||||
expect(number(i18n)(13.1)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('number should accept large number', () => {
|
||||
expect(number(i18n)(999999999999999999999.9)).toBeUndefined();
|
||||
expect(number(i18n)(-999999999999999999999.9)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('number should reject string containing alphanum', () => {
|
||||
expect(number(i18n)('15a')).toEqual({
|
||||
id: 'This field must be a number',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user