Skip to content

Commit 6ff563b

Browse files
authored
Merge pull request #9 from petruki/master
feat: Added Regex Validation
2 parents 253d37f + 1bf6cfa commit 6ff563b

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/utils/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const StrategiesType = Object.freeze({
1717
VALUE: 'VALUE_VALIDATION',
1818
NUMERIC: 'NUMERIC_VALIDATION',
1919
TIME: 'TIME_VALIDATION',
20-
DATE: 'DATE_VALIDATION'
20+
DATE: 'DATE_VALIDATION',
21+
REGEX: 'REGEX_VALIDATION'
2122
});
2223

2324
const OperationsType = Object.freeze({
@@ -42,6 +43,8 @@ const processOperation = (strategy, operation, input, values) => {
4243
return processTime(operation, input, values);
4344
case StrategiesType.DATE:
4445
return processDate(operation, input, values);
46+
case StrategiesType.REGEX:
47+
return processREGEX(operation, input, values);
4548
}
4649
}
4750

@@ -136,6 +139,29 @@ function processDate(operation, input, values) {
136139
}
137140
}
138141

142+
function processREGEX(operation, input, values) {
143+
switch(operation) {
144+
case OperationsType.EXIST:
145+
for (var i = 0; i < values.length; i++) {
146+
if (input.match(values[i])) {
147+
return true;
148+
}
149+
}
150+
return false;
151+
case OperationsType.NOT_EXIST:
152+
for (var i = 0; i < values.length; i++) {
153+
if (input.match(values[i])) {
154+
return false;
155+
}
156+
}
157+
return true;
158+
case OperationsType.EQUAL:
159+
return input.match(`\\b${values[0]}\\b`) != null;
160+
case OperationsType.NOT_EQUAL:
161+
return input.match(`\\b${values[0]}\\b`) == null;
162+
}
163+
}
164+
139165
module.exports = {
140166
loadDomain,
141167
processOperation,

test/test-strategy-operations.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,79 @@ describe('Processing strategy: DATE', () => {
374374
assert.isTrue(result);
375375
})
376376

377+
})
378+
379+
describe('Processing strategy: REGEX', () => {
380+
const mock_values1 = [
381+
'\\bUSER_[0-9]{1,2}\\b'
382+
];
383+
384+
const mock_values2 = [
385+
'\\bUSER_[0-9]{1,2}\\b', '\\buser-[0-9]{1,2}\\b'
386+
];
387+
388+
const mock_values3 = [
389+
'USER_[0-9]{1,2}'
390+
];
391+
392+
it('UNIT_STRATEGY_SUITE - Should agree when expect to exist using EXIST operation', () => {
393+
let result = processOperation(
394+
StrategiesType.REGEX, OperationsType.EXIST, 'USER_1', mock_values1);
395+
assert.isTrue(result);
396+
397+
result = processOperation(
398+
StrategiesType.REGEX, OperationsType.EXIST, 'user-01', mock_values2);
399+
assert.isTrue(result);
400+
})
401+
402+
it('UNIT_STRATEGY_SUITE - Should NOT agree when expect to exist using EXIST operation', () => {
403+
let result = processOperation(
404+
StrategiesType.REGEX, OperationsType.EXIST, 'USER_123', mock_values1);
405+
assert.isFalse(result);
406+
407+
//mock_values3 does not require exact match
408+
result = processOperation(
409+
StrategiesType.REGEX, OperationsType.EXIST, 'USER_123', mock_values3);
410+
assert.isTrue(result);
411+
})
412+
413+
it('UNIT_STRATEGY_SUITE - Should agree when expect to not exist using NOT_EXIST operation', () => {
414+
let result = processOperation(
415+
StrategiesType.REGEX, OperationsType.NOT_EXIST, 'USER_123', mock_values1);
416+
assert.isTrue(result);
417+
418+
result = processOperation(
419+
StrategiesType.REGEX, OperationsType.NOT_EXIST, 'user-123', mock_values2);
420+
assert.isTrue(result);
421+
})
422+
423+
it('UNIT_STRATEGY_SUITE - Should NOT agree when expect to not exist using NOT_EXIST operation', () => {
424+
const result = processOperation(
425+
StrategiesType.REGEX, OperationsType.NOT_EXIST, 'USER_12', mock_values1);
426+
assert.isFalse(result);
427+
})
428+
429+
it('UNIT_STRATEGY_SUITE - Should agree when expect to be equal using EQUAL operation', () => {
430+
const result = processOperation(
431+
StrategiesType.REGEX, OperationsType.EQUAL, 'USER_11', mock_values3);
432+
assert.isTrue(result);
433+
})
434+
435+
it('UNIT_STRATEGY_SUITE - Should NOT agree when expect to be equal using EQUAL operation', () => {
436+
const result = processOperation(
437+
StrategiesType.REGEX, OperationsType.EQUAL, 'user-11', mock_values3);
438+
assert.isFalse(result);
439+
})
440+
441+
it('UNIT_STRATEGY_SUITE - Should agree when expect to not be equal using NOT_EQUAL operation', () => {
442+
const result = processOperation(
443+
StrategiesType.REGEX, OperationsType.NOT_EQUAL, 'USER_123', mock_values3);
444+
assert.isTrue(result);
445+
})
446+
447+
it('UNIT_STRATEGY_SUITE - Should NOT agree when expect to not be equal using NOT_EQUAL operation', () => {
448+
const result = processOperation(
449+
StrategiesType.REGEX, OperationsType.NOT_EQUAL, 'USER_1', mock_values3);
450+
assert.isFalse(result);
451+
})
377452
})

0 commit comments

Comments
 (0)