Skip to content

Commit

Permalink
fix(core) Properly handles flags in string regex validators
Browse files Browse the repository at this point in the history
Signed-off-by: jeromesimeon <[email protected]>
  • Loading branch information
jeromesimeon committed Aug 4, 2021
1 parent 3cd19ca commit cb3a82e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
8 changes: 1 addition & 7 deletions packages/concerto-core/lib/introspect/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions packages/concerto-core/lib/introspect/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,7 @@ UnicodeEscapeSequence

RegularExpressionLiteral "regular expression"
= "/" pattern:$RegularExpressionBody "/" flags:$RegularExpressionFlags {
var value;

try {
value = new RegExp(pattern, flags);
} catch (e) {
error(e.message);
}
var value = `/${pattern}/${flags}`;

return { type: "Literal", value: value };
}
Expand Down
16 changes: 12 additions & 4 deletions packages/concerto-core/lib/introspect/stringvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ class StringValidator extends Validator{
* @throws {IllegalModelException}
*/
constructor(field, validator) {
super(field,validator);
super(field, validator);
try {
// discard the leading / and closing /
this.regex = new RegExp(validator.substring(1,validator.length-1));
// Extract the RegExp components (pattern and flag)
const match = validator.match(/^\/([^/]*)\/(.*)$/);
const pattern = match[1];
const flags = match[2];

if (flags) {
this.regex = new RegExp(pattern, flags);
} else {
this.regex = new RegExp(pattern);
}
}
catch(exception) {
this.reportError(exception.message);
Expand All @@ -53,7 +61,7 @@ class StringValidator extends Validator{
validate(identifier, value) {
if(value !== null) {
if(!this.regex.test(value)) {
this.reportError(identifier, 'Value + \'' + value + '\' failed to match validation regex: ' + this.regex);
this.reportError(identifier, 'Value \'' + value + '\' failed to match validation regex: ' + this.regex);
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions packages/concerto-core/test/introspect/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Field', () => {
should.equal(f.validator, null);
});

it('should save the incoming validator', () => {
it('should save the incoming string validator', () => {

let f = new Field(mockClassDeclaration, {
id: {
Expand All @@ -52,7 +52,21 @@ describe('Field', () => {
propertyType: {
name: 'String'
},
regex: 'suchValidator'
regex: '/^suchValidator$/'
});
f.getValidator().validate('id', 'suchValidator');
});

it('should save the incoming string validator (with flag)', () => {

let f = new Field(mockClassDeclaration, {
id: {
name: 'field',
},
propertyType: {
name: 'String'
},
regex: '/^suchValidator$/u'
});
f.getValidator().validate('id', 'suchValidator');
});
Expand Down

0 comments on commit cb3a82e

Please sign in to comment.