Skip to content
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
63 changes: 32 additions & 31 deletions packages/concerto-cto/src/parser.js

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

63 changes: 32 additions & 31 deletions packages/concerto-cto/src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@
const primitiveTypes = ['Boolean', 'String', 'DateTime', 'Double', 'Integer', 'Long'];
return (primitiveTypes.indexOf(typeName) >= 0);
}
function bucketValidators(items, kindByClass) {
const result = {};
for (const v of items) {
const kind = kindByClass[v.$class];
if (result[kind]) {
error(`Duplicate ${kind} validator`);
}
result[kind] = v;
}
return result;
}
}

Start
Expand Down Expand Up @@ -1435,53 +1446,43 @@ LongDomainValidator

StringScalarValidators
= items:(__ v:(StringRegexValidator / StringLengthValidator) { return v; })* {
const result = {};
for (const v of items) {
if (v.$class === `${metamodelNamespace}.StringRegexValidator`) { result.regex = v; }
else if (v.$class === `${metamodelNamespace}.StringLengthValidator`) { result.length = v; }
}
return result;
return bucketValidators(items, {
[`${metamodelNamespace}.StringRegexValidator`]: 'regex',
[`${metamodelNamespace}.StringLengthValidator`]: 'length',
});
}

StringFieldValidators
= items:(__ v:(StringRegexValidator / StringLengthValidator / CollectionSizeValidator) { return v; })* {
const result = {};
for (const v of items) {
if (v.$class === `${metamodelNamespace}.StringRegexValidator`) { result.regex = v; }
else if (v.$class === `${metamodelNamespace}.StringLengthValidator`) { result.length = v; }
else if (v.$class === `${metamodelNamespace}.CollectionSizeValidator`) { result.size = v; }
}
return result;
return bucketValidators(items, {
[`${metamodelNamespace}.StringRegexValidator`]: 'regex',
[`${metamodelNamespace}.StringLengthValidator`]: 'length',
[`${metamodelNamespace}.CollectionSizeValidator`]: 'size',
});
}

RealFieldValidators
= items:(__ v:(RealDomainValidator / CollectionSizeValidator) { return v; })* {
const result = {};
for (const v of items) {
if (v.$class === `${metamodelNamespace}.DoubleDomainValidator`) { result.range = v; }
else if (v.$class === `${metamodelNamespace}.CollectionSizeValidator`) { result.size = v; }
}
return result;
return bucketValidators(items, {
[`${metamodelNamespace}.DoubleDomainValidator`]: 'range',
[`${metamodelNamespace}.CollectionSizeValidator`]: 'size',
});
}

IntegerFieldValidators
= items:(__ v:(IntegerDomainValidator / CollectionSizeValidator) { return v; })* {
const result = {};
for (const v of items) {
if (v.$class === `${metamodelNamespace}.IntegerDomainValidator`) { result.range = v; }
else if (v.$class === `${metamodelNamespace}.CollectionSizeValidator`) { result.size = v; }
}
return result;
return bucketValidators(items, {
[`${metamodelNamespace}.IntegerDomainValidator`]: 'range',
[`${metamodelNamespace}.CollectionSizeValidator`]: 'size',
});
}

LongFieldValidators
= items:(__ v:(LongDomainValidator / CollectionSizeValidator) { return v; })* {
const result = {};
for (const v of items) {
if (v.$class === `${metamodelNamespace}.LongDomainValidator`) { result.range = v; }
else if (v.$class === `${metamodelNamespace}.CollectionSizeValidator`) { result.size = v; }
}
return result;
return bucketValidators(items, {
[`${metamodelNamespace}.LongDomainValidator`]: 'range',
[`${metamodelNamespace}.CollectionSizeValidator`]: 'size',
});
}

RealFieldDeclaration
Expand Down
42 changes: 42 additions & 0 deletions packages/concerto-cto/test/parserMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,48 @@ describe('parser', () => {
const ast2 = Parser.parse('namespace t@1.0.0\nconcept A { o String[] x size=[1,5] length=[1,10] regex=/abc/ }', undefined, { skipLocationNodes: true });
ast1.should.deep.equal(ast2);
});

it('Should not parse a duplicate regex validator on a String field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o String x regex=/a/ regex=/b/ }');
}).should.throw(/Duplicate regex validator/);
});

it('Should not parse a duplicate length validator on a String field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o String x length=[1,2] length=[30,40] }');
}).should.throw(/Duplicate length validator/);
});

it('Should not parse a duplicate range validator on an Integer field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o Integer x range=[0,10] range=[500,600] }');
}).should.throw(/Duplicate range validator/);
});

it('Should not parse a duplicate range validator on a Double field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o Double x range=[0.5,1.5] range=[2.5,3.5] }');
}).should.throw(/Duplicate range validator/);
});

it('Should not parse a duplicate range validator on a Long field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o Long x range=[0,10] range=[500,600] }');
}).should.throw(/Duplicate range validator/);
});

it('Should not parse a duplicate size validator on a String array field', () => {
(() => {
Parser.parse('namespace t@1.0.0\nconcept A { o String[] x size=[1,2] size=[9,10] }');
}).should.throw(/Duplicate size validator/);
});

it('Should not parse a duplicate regex validator on a String scalar', () => {
(() => {
Parser.parse('namespace t@1.0.0\nscalar S extends String regex=/a/ regex=/b/');
}).should.throw(/Duplicate regex validator/);
});
});

describe('identifiers', () => {
Expand Down
Loading