Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core) Properly handles flags in string regex validators #303

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 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.

15 changes: 5 additions & 10 deletions packages/concerto-core/lib/introspect/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,10 @@ UnicodeEscapeSequence

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

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

return { type: "Literal", value: value };
return {
pattern,
flags
};
}

RegularExpressionBody
Expand Down Expand Up @@ -1037,7 +1032,7 @@ StringFieldDeclaration
}

StringRegexValidator
= "regex" __ "=" __ regex:$RegularExpressionLiteral {
= "regex" __ "=" __ regex:RegularExpressionLiteral {
return regex
}

Expand Down
11 changes: 7 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,13 @@ 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));
if (validator.flags) {
this.regex = new RegExp(validator.pattern, validator.flags);
} else {
this.regex = new RegExp(validator.pattern);
}
}
catch(exception) {
this.reportError(exception.message);
Expand All @@ -53,7 +56,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
33 changes: 29 additions & 4 deletions packages/concerto-core/test/introspect/stringvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('StringValidator', () => {

it('should throw for invalid regexes', () => {
(() => {
new StringValidator(mockField, '/^[A-z/' );
new StringValidator(mockField, { pattern: '^[A-z' });
}).should.throw(/Validator error for field/);
});

Expand All @@ -42,22 +42,47 @@ describe('StringValidator', () => {
describe('#validate', () => {

it('should ignore a null string', () => {
let v = new StringValidator(mockField, '/^[A-z][A-z][0-9]{7}/' );
let v = new StringValidator(mockField, { pattern: '^[A-z][A-z][0-9]{7}' });
v.getRegex().toString().should.equal('/^[A-z][A-z][0-9]{7}/');
v.validate('id', null);
});

it('should validate a string', () => {
let v = new StringValidator(mockField, '/^[A-z][A-z][0-9]{7}/' );
let v = new StringValidator(mockField, { pattern: '^[A-z][A-z][0-9]{7}' });
v.validate('id', 'AB1234567');
});

it('should detect mismatch string', () => {
let v = new StringValidator(mockField, '/^[A-z][A-z][0-9]{7}/');
let v = new StringValidator(mockField, { pattern: '^[A-z][A-z][0-9]{7}' });

(() => {
v.validate('id', 'xyz');
}).should.throw(/Validator error for field id org.acme.myField/);
});

it('should validate a string with escaped chacters', () => {
let v = new StringValidator(mockField, { pattern: '^[\\\\]*\\n$' });
v.validate('id', '\\\\\n');
});

it('should not validate a string with escaped chacters', () => {
let v = new StringValidator(mockField, { pattern: '^[\\\\]*\\n$' });
(() => {
v.validate('id', '\\hi!\n');
}).should.throw(/Validator error for field id org.acme.myField/);
});

it('should validate a unicode string', () => {
let v = new StringValidator(mockField, { pattern: '^(?!null|true|false)(\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4})(?:\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4}|\\p{Mn}|\\p{Mc}|\\p{Nd}|\\p{Pc}|\\u200C|\\u200D)*$', flags: 'u' });
v.validate('id', 'AB1234567');
});

it('should not validate a unicode string', () => {
let v = new StringValidator(mockField, { pattern: '^(?!null|true|false)(\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4})(?:\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\$|_|\\\\u[0-9A-Fa-f]{4}|\\p{Mn}|\\p{Mc}|\\p{Nd}|\\p{Pc}|\\u200C|\\u200D)*$', flags: 'u' });
(() => {
v.validate('id', '1FOO');
}).should.throw(/Validator error for field id org.acme.myField/);
});

});
});