Skip to content

Commit

Permalink
validate regex
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Dec 13, 2021
1 parent c16111a commit e55442e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {NumberValidator} from './js/validators/NumberValidator';
import {IntegerValidator} from './js/validators/IntegerValidator';
import {DecimalValidator} from './js/validators/DecimalValidator';
import {ConfirmationValidator} from './js/validators/ConfirmationValidator.js';
import {RegexValidator} from './js/validators/RegexValidator.js';

function testSuccess(response)
{
Expand Down Expand Up @@ -243,6 +244,26 @@ test(
}
);

test(
'RegexValidator',
() =>
{
let v = new RegexValidator('not valid regex');
testFailure(v.validate('test'), ['not a valid regular expression']);

v = new RegexValidator({});
testFailure(v.validate('test'), ['not a valid regular expression']);

v = new RegexValidator('/test/', 'not test');
testFailure(v.validate('abc'), ['not test']);

v = new RegexValidator('/test/');
testFailure(v.validate('abc'), ['does not match regular expression']);
testFailure(v.validate('1'), ['does not match regular expression']);
testSuccess(v.validate('test'));
}
);

test(
'IntegerValidator',
() =>
Expand Down
4 changes: 4 additions & 0 deletions js/validators/RegexValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class RegexValidator extends Validator
if(typeof regex === 'string')
{
const parts = /\/(.*)\/(.*)/.exec(regex);
if(!parts)
{
return ValidationResponse.error(['not a valid regular expression']);
}
regex = new RegExp(parts[1], parts[2]);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@packaged/validate",
"version": "3.0.2",
"version": "3.0.3",
"main": "index.js",
"repository": "[email protected]:packaged/validate",
"author": "Tom Kay <[email protected]>",
Expand Down

0 comments on commit e55442e

Please sign in to comment.