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
3 changes: 2 additions & 1 deletion packages/concertino/scripts/generateTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class ConcertinoTypescriptVisitor extends CodeGen.TypescriptVisitor {
visitField(field, parameters) {
const typeOverride = field.getDecorators().find(decorator => decorator.getName() === OVERRIDE_DECORATOR);
if (typeOverride) {
parameters.fileWriter.writeLine(1, field.getName() + ': ' + typeOverride.getArguments()[0] + ';');
const optional = field.isOptional() ? '?' : '';
parameters.fileWriter.writeLine(1, field.getName() + optional + ': ' + typeOverride.getArguments()[0] + ';');
return null;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/concertino/src/concertinoSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ function extractMetaProperties(property: PropertyUnion | ScalarDeclarationUnion,
const max = property.lengthValidator.maxLength === undefined ? null : property.lengthValidator.maxLength;
(propertyEntry as IConcertinoStringProperty).length = [min, max];
}
if ('sizeValidator' in property && (property as any).sizeValidator) {
const sv = (property as any).sizeValidator;
const min = sv.minSize === undefined ? null : sv.minSize;
const max = sv.maxSize === undefined ? null : sv.maxSize;
(propertyEntry as IConcertinoProperty).size = [min, max];
}
if ('defaultValue' in property && property.defaultValue !== undefined && property.defaultValue !== null) {
(propertyEntry as IConcertinoStringProperty | IConcertinoIntegerProperty | IConcertinoDoubleProperty | IConcertinoLongProperty | IConcertinoBooleanProperty | IConcertinoDateTimeProperty)
.default = property.defaultValue;
Expand Down
12 changes: 12 additions & 0 deletions packages/concertino/src/metamodelSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import {
IBooleanProperty,
IBooleanScalar,
ICollectionSizeValidator,
IConceptDeclaration,
IDecorator,
IDoubleDomainValidator,
Expand Down Expand Up @@ -382,6 +383,17 @@ function transformProperties(properties: Record<string, IConcertinoProperty>): {
(result as IStringProperty).lengthValidator = lengthValidator;
}

if (property.size) {
const sizeValidator: ICollectionSizeValidator = { $class: 'concerto.metamodel@1.0.0.CollectionSizeValidator' };
if (property.size[0] !== undefined && property.size[0] !== null) {
sizeValidator.minSize = property.size[0];
}
if (property.size[1] !== undefined && property.size[1] !== null) {
sizeValidator.maxSize = property.size[1];
}
(result as any).sizeValidator = sizeValidator;
}

if (stringProperty.default !== undefined) {
(result as IStringProperty | IIntegerProperty | ILongProperty | IDoubleProperty | IBooleanProperty).defaultValue = stringProperty.default;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/concertino/src/spec/concertino.cto
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ abstract concept ConcertinoProperty {
o Identifier inheritedFrom optional // For inherited properties
o Boolean isCircular optional
o Boolean isEnum optional
@CodeGen_TypeScript_Override("[number | null, number | null]")
o Integer[] size optional
o Vocabulary vocabulary optional
o MetadataMap metadata optional
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ IConcertinoDateTimeScalarDeclaration;

export interface IConcertinoStringScalarDeclaration extends IConcertinoScalarDeclaration {
regex?: string;
length: [number | null, number | null];
length?: [number | null, number | null];
default?: string;
}

export interface IConcertinoIntegerScalarDeclaration extends IConcertinoScalarDeclaration {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

Expand All @@ -99,12 +99,12 @@ export interface IConcertinoBooleanScalarDeclaration extends IConcertinoScalarDe
}

export interface IConcertinoDoubleScalarDeclaration extends IConcertinoScalarDeclaration {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

export interface IConcertinoLongScalarDeclaration extends IConcertinoScalarDeclaration {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

Expand Down Expand Up @@ -141,6 +141,7 @@ export interface IConcertinoProperty extends IConcept {
inheritedFrom?: string;
isCircular?: boolean;
isEnum?: boolean;
size?: [number | null, number | null];
vocabulary?: IVocabulary;
metadata?: MetadataMap;
}
Expand All @@ -154,12 +155,12 @@ IConcertinoDateTimeProperty;

export interface IConcertinoStringProperty extends IConcertinoProperty {
regex?: string;
length: [number | null, number | null];
length?: [number | null, number | null];
default?: string;
}

export interface IConcertinoIntegerProperty extends IConcertinoProperty {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

Expand All @@ -168,12 +169,12 @@ export interface IConcertinoBooleanProperty extends IConcertinoProperty {
}

export interface IConcertinoDoubleProperty extends IConcertinoProperty {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

export interface IConcertinoLongProperty extends IConcertinoProperty {
range: [number | null, number | null];
range?: [number | null, number | null];
default?: number;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/concertino/src/spec/concertino.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@
"minItems": 2,
"maxItems": 2
},
"size": {
"type": "array",
"items": [
{ "oneOf": [{ "type": "number" }, { "type": "null" }] },
{ "oneOf": [{ "type": "number" }, { "type": "null" }] }
],
"minItems": 2,
"maxItems": 2
},
"default": {
"oneOf": [
{ "type": "string" },
Expand Down
8 changes: 7 additions & 1 deletion packages/concertino/src/spec/concerto.metamodel@1.0.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export interface IRange extends IConcept {

export interface ITypeIdentifier extends IConcept {
name: string;
resolvedName?: string;
namespace?: string;
resolvedName?: string;
}

export interface IDecoratorLiteral extends IConcept {
Expand Down Expand Up @@ -195,6 +195,7 @@ export interface IProperty extends IConcept {
name: string;
isArray: boolean;
isOptional: boolean;
sizeValidator?: ICollectionSizeValidator;
decorators?: IDecorator[];
location?: IRange;
}
Expand All @@ -208,6 +209,11 @@ IDoubleProperty |
IIntegerProperty |
ILongProperty;

export interface ICollectionSizeValidator extends IConcept {
minSize?: number;
maxSize?: number;
}

export interface IRelationshipProperty extends IProperty {
type: ITypeIdentifier;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/concertino/src/spec/concerto@1.0.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
IMapValueType,
IEnumProperty,
IProperty,
ICollectionSizeValidator,
IStringRegexValidator,
IStringLengthValidator,
IDoubleDomainValidator,
Expand Down Expand Up @@ -57,6 +58,7 @@ IMapKeyType |
IMapValueType |
IEnumProperty |
IProperty |
ICollectionSizeValidator |
IStringRegexValidator |
IStringLengthValidator |
IDoubleDomainValidator |
Expand Down
56 changes: 56 additions & 0 deletions packages/concertino/test/__snapshots__/concertino.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,56 @@ exports[`concertino roundtripping (sample models) > should produce valid concert
},
"type": "ConceptDeclaration",
},
"org.example.collectionsize@1.0.0.PhoneBook": {
"key": {
"type": "String",
},
"type": "MapDeclaration",
"value": {
"type": "String",
},
},
"org.example.collectionsize@1.0.0.Team": {
"properties": {
"contacts": {
"name": "contacts",
"size": [
1,
10,
],
"type": "org.example.collectionsize@1.0.0.PhoneBook",
},
"middle": {
"isArray": true,
"isOptional": true,
"name": "middle",
"size": [
null,
3,
],
"type": "String",
},
"scores": {
"isArray": true,
"name": "scores",
"size": [
1,
null,
],
"type": "Integer",
},
"tags": {
"isArray": true,
"name": "tags",
"size": [
2,
5,
],
"type": "String",
},
},
"type": "ConceptDeclaration",
},
"org.example.complex@1.0.0.Department": {
"properties": {
"code": {
Expand Down Expand Up @@ -3328,6 +3378,12 @@ exports[`concertino roundtripping (sample models) > should produce valid concert
"imports": [],
"sourceUri": undefined,
},
"org.example.collectionsize@1.0.0": {
"concertoVersion": undefined,
"decorators": [],
"imports": [],
"sourceUri": undefined,
},
"org.example.complex@1.0.0": {
"concertoVersion": undefined,
"decorators": [],
Expand Down
48 changes: 48 additions & 0 deletions packages/concertino/test/concertino.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,54 @@ describe('concertino edge cases and error handling', () => {
}).toThrow(/Unsupported object type/);
});

it('should roundtrip sizeValidator on array and map properties', () => {
const converter = new ConcertinoConverter();
const ast = {
$class: 'concerto.metamodel@1.0.0.Models',
models: [{
$class: 'concerto.metamodel@1.0.0.Model',
namespace: 'org.size@1.0.0',
declarations: [{
$class: 'concerto.metamodel@1.0.0.ConceptDeclaration',
name: 'Thing',
isAbstract: false,
properties: [
{
$class: 'concerto.metamodel@1.0.0.StringProperty',
name: 'tags',
isArray: true,
isOptional: false,
sizeValidator: {
$class: 'concerto.metamodel@1.0.0.CollectionSizeValidator',
minSize: 1,
maxSize: 10
}
},
{
$class: 'concerto.metamodel@1.0.0.IntegerProperty',
name: 'scores',
isArray: true,
isOptional: true,
sizeValidator: {
$class: 'concerto.metamodel@1.0.0.CollectionSizeValidator',
minSize: 2
}
}
]
}]
}]
};

const concertino = converter.fromConcertoMetamodel(ast);
const declarations = concertino.declarations as any;
const props = declarations['org.size@1.0.0.Thing'].properties;
expect(props.tags.size).toStrictEqual([1, 10]);
expect(props.scores.size).toStrictEqual([2, null]);

const metamodel = converter.toConcertoMetamodel(concertino);
expect(metamodel).toStrictEqual(ast);
});

it('should throw for parent === undefined in getInheritanceChain', () => {
expect(() => {
// @ts-expect-error purposely invalid
Expand Down
13 changes: 13 additions & 0 deletions packages/concertino/test/cto/collectionsize.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace org.example.collectionsize@1.0.0

map PhoneBook {
o String
o String
}

concept Team {
o String[] tags size=[2,5]
o Integer[] scores size=[1,]
o String[] middle size=[,3] optional
o PhoneBook contacts size=[1,10]
}
8 changes: 8 additions & 0 deletions packages/concerto-core/src/serializer/resourcevalidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ class ResourceValidator {
this.checkArray(obj, field,parameters);
}
else {
if(field.getSizeValidator() && obj instanceof Map) {
field.getSizeValidator().validate(parameters.rootResourceIdentifier, obj.size);
}
this.checkItem(obj, field,parameters);
}
}
Expand All @@ -338,6 +341,7 @@ class ResourceValidator {
const enumDeclaration = field.getParent().getModelFile().getType(field.getType());

if(field.isArray()) {
field.getSizeValidator()?.validate(parameters.rootResourceIdentifier, obj.length);
for(let n=0; n < obj.length; n++) {
const item = obj[n];
parameters.stack.push(item);
Expand All @@ -364,6 +368,8 @@ class ResourceValidator {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, field.getName(), obj, field);
}

field.getSizeValidator()?.validate(parameters.rootResourceIdentifier, obj.length);

for(let n=0; n < obj.length; n++) {
const item = obj[n];
this.checkItem(item, field, parameters);
Expand Down Expand Up @@ -462,6 +468,8 @@ class ResourceValidator {
ResourceValidator.reportInvalidFieldAssignment(parameters.rootResourceIdentifier, relationshipDeclaration.getName(), obj, relationshipDeclaration);
}

relationshipDeclaration.getSizeValidator()?.validate(parameters.rootResourceIdentifier, obj.length);

for(let n=0; n < obj.length; n++) {
const item = obj[n];
this.checkRelationship(parameters, relationshipDeclaration, item);
Expand Down
Loading
Loading