Skip to content

Commit

Permalink
fix(#9604): fix integer validation in sms rules (backport)
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen authored Nov 4, 2024
1 parent d18a402 commit 69ae8c0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 15 deletions.
20 changes: 5 additions & 15 deletions shared-libs/validation/src/validator_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const re = {
};

const ValidatorFunctions = {
equals: (allValues, value, equalsTo) => value === equalsTo,
equals: (allValues, value, equalsTo) => value == equalsTo, // eslint-disable-line eqeqeq

iequals: (allValues, value, equalsTo) => value.toLowerCase() === equalsTo.toLowerCase(),
iequals: (allValues, value, equalsTo) => value.toLowerCase() == equalsTo.toLowerCase(), // eslint-disable-line eqeqeq

sequals: (allValues, value, equalsTo) => value === equalsTo,

Expand All @@ -28,17 +28,7 @@ const ValidatorFunctions = {
return ((numVal >= min) && (numVal <= max));
},

in: (allValues, value) => {
const args = Array.prototype.slice.call(arguments);
args.shift();
args.shift();
for (const arg of args) {
if (arg === value) {
return true;
}
}
return false;
},
in: (allValues, value, ...args) => args.some(arg => arg == value), // eslint-disable-line eqeqeq

required: (allValues, value) => !!value,

Expand All @@ -64,9 +54,9 @@ const ValidatorFunctions = {
return (new RegExp(regex, flags)).test(value);
},

integer: (allValues, value) => parseInt(value, 10) === value,
integer: (allValues, value) => parseInt(value, 10) == value, // eslint-disable-line eqeqeq

equalsto: (allValues, value, equalsToKey) => value === allValues[equalsToKey]
equalsto: (allValues, value, equalsToKey) => value == allValues[equalsToKey], // eslint-disable-line eqeqeq
};

module.exports = ValidatorFunctions;
23 changes: 23 additions & 0 deletions shared-libs/validation/test/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,5 +950,28 @@ describe('validations', () => {
});

});

it('should validate integers', () => {
const validations = [
{
property: 'lmp_year',
rule: '(integer && min(2078) && max(2090))',
translation_key: 'registration.lmp_date.year.incorrect'
},
{
property: 'lmp_month',
rule: '(integer && min(1) && max(12))',
translation_key: 'registration.lmp_date.month.incorrect'
}
];
const doc = {
_id: 'same',
lmp_year: '2080',
lmp_month: '10'
};
return validation.validate(doc, validations).then(errors => {
assert.deepEqual(errors, []);
});
});

});
127 changes: 127 additions & 0 deletions shared-libs/validation/test/validator_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const assert = require('chai').assert;
const validatorFunctions = require('../src/validator_functions');

describe('validator functions', () => {

describe('integer', () => {

const valid = ['0', '1', '2080', '-2', ' 5'];

valid.forEach(value => {
it(`should return true for "${value}"`, () => {
assert.isTrue(validatorFunctions.integer(null, value));
});
});

const invalid = ['', 'a', '56b', '2.3', '-'];

invalid.forEach(value => {
it(`should return false for "${value}"`, () => {
assert.isFalse(validatorFunctions.integer(null, value));
});
});
});

describe('in', () => {

it('should return true for value in list', () => {
assert.isTrue(validatorFunctions.in(null, 'a', 'a', 'b', 'c'));
});

it('should return false for value not in list', () => {
assert.isFalse(validatorFunctions.in(null, 'd', 'a', 'b', 'c'));
});

});

describe('equals', () => {

it('should return true for equal values same type', () => {
assert.isTrue(validatorFunctions.equals(null, 'a', 'a'));
});

it('should return true for equal values different type', () => {
assert.isTrue(validatorFunctions.equals(null, '1', 1));
});

it('should return false for equal values difference case', () => {
assert.isFalse(validatorFunctions.equals(null, 'a', 'A'));
});

it('should return false for unequal values', () => {
assert.isFalse(validatorFunctions.equals(null, 'a', 'b'));
});

});

describe('iequals', () => {

it('should return true for equal values same type', () => {
assert.isTrue(validatorFunctions.iequals(null, 'a', 'a'));
});

it('should return false for equal values difference case', () => {
assert.isTrue(validatorFunctions.iequals(null, 'a', 'A'));
});

it('should return false for unequal values', () => {
assert.isFalse(validatorFunctions.iequals(null, 'a', 'b'));
});

});

describe('sequals', () => {

it('should return true for equal values same type', () => {
assert.isTrue(validatorFunctions.sequals(null, 'a', 'a'));
});

it('should return false for equal values different type', () => {
assert.isFalse(validatorFunctions.sequals(null, '1', 1));
});

it('should return false for equal values difference case', () => {
assert.isFalse(validatorFunctions.sequals(null, 'a', 'A'));
});

it('should return false for unequal values', () => {
assert.isFalse(validatorFunctions.sequals(null, 'a', 'b'));
});

});


describe('siequals', () => {

it('should return true for equal values same type', () => {
assert.isTrue(validatorFunctions.siequals(null, 'a', 'a'));
});

it('should return true for equal values difference case', () => {
assert.isTrue(validatorFunctions.siequals(null, 'a', 'A'));
});

it('should return false for unequal values', () => {
assert.isFalse(validatorFunctions.siequals(null, 'a', 'b'));
});

});

describe('equalsto', () => {

const allValues = {
name: 'gareth',
location: 'darmstadt'
};

it('should return true for equal values', () => {
assert.isTrue(validatorFunctions.equalsto(allValues, 'gareth', 'name'));
});

it('should return false for unequal values', () => {
assert.isFalse(validatorFunctions.equalsto(allValues, 'gareth', 'location'));
});

});

});

0 comments on commit 69ae8c0

Please sign in to comment.