Skip to content
Open
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
59 changes: 59 additions & 0 deletions packages/cli/test/ts-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,63 @@ model Post {
plugins: {},
});
});

it('supports implicit conversions from enums to arrays', async () => {
const { schema } = await generateTsSchema(`
enum PostStatus {
DRAFT
ACTIVE
CANCELLED
}

model User {
id Int @id @default(autoincrement())
}

model Post {
id String @id
status String

@@validate(status in PostStatus)
}
`);

expect(schema.models['Post']?.attributes).toMatchObject([
{
name: '@@validate',
args: [
{
name: 'value',
value: {
kind: 'binary',
op: 'in',
left: {
kind: 'field',
field: 'status',
},
right: {
kind: 'array',
type: 'PostStatus',
items: [
{
kind: 'literal',
value: 'DRAFT',
},
{
kind: 'literal',
value: 'ACTIVE',
},
{
kind: 'literal',
value: 'CANCELLED',
},
],
},
binding: undefined,
},
},
],
},
]);
});
});
4 changes: 2 additions & 2 deletions packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ export function isReferenceExpr(item: unknown): item is ReferenceExpr {
return reflection.isInstance(item, ReferenceExpr.$type);
}

export type ReferenceTarget = CollectionPredicateBinding | DataField | EnumField | FunctionParam;
export type ReferenceTarget = CollectionPredicateBinding | DataField | Enum | EnumField | FunctionParam;

export const ReferenceTarget = {
$type: 'ReferenceTarget'
Expand Down Expand Up @@ -1427,7 +1427,7 @@ export class ZModelAstReflection extends langium.AbstractAstReflection {
name: Enum.name
}
},
superTypes: [AbstractDeclaration.$type, TypeDeclaration.$type]
superTypes: [AbstractDeclaration.$type, ReferenceTarget.$type, TypeDeclaration.$type]
},
EnumField: {
name: EnumField.$type,
Expand Down
6 changes: 6 additions & 0 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4008,6 +4008,12 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"typeRef": {
"$ref": "#/rules@30"
}
},
{
"$type": "SimpleType",
"typeRef": {
"$ref": "#/rules@46"
}
}
]
}
Expand Down
6 changes: 6 additions & 0 deletions packages/language/src/zmodel-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ export class ZModelLinker extends DefaultLinker {
} else if (isDataField(target) || isFunctionParam(target)) {
// other references are resolved to their declared type
this.resolveToDeclaredType(node, target.type);
} else if (isEnum(target)) {
node.$resolvedType = {
decl: target,
array: true,
nullable: false,
};
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ConfigArrayExpr:
ConfigExpr:
LiteralExpr | InvocationExpr | ConfigArrayExpr;

type ReferenceTarget = FunctionParam | DataField | EnumField | CollectionPredicateBinding;
type ReferenceTarget = FunctionParam | DataField | EnumField | CollectionPredicateBinding | Enum;

ThisExpr:
value='this';
Expand Down
26 changes: 26 additions & 0 deletions packages/language/test/attribute-application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,30 @@ describe('Attribute application validation tests', () => {
/relation "bar" is not optional/,
);
});

it('@@validate accepts implicit array conversions from enum references', async () => {
await loadSchema(`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

enum PostStatus {
DRAFT
ACTIVE
CANCELLED
}

model User {
id Int @id @default(autoincrement())
}

model Post {
id String @id
status String

@@validate(status in PostStatus)
}
`);
});
});
10 changes: 10 additions & 0 deletions packages/sdk/src/ts-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,16 @@ export class TsSchemaGenerator {
.when(isCollectionPredicateBinding, () =>
this.createExpressionUtilsCall('binding', [this.createLiteralNode(expr.target.$refText)]),
)
.when(isEnum, () =>
this.createExpressionUtilsCall('array', [
this.createLiteralNode(expr.target.$refText),
ts.factory.createArrayLiteralExpression(
(target as Enum).fields.map((field) =>
this.createLiteralExpression('StringLiteral', field.name),
),
),
]),
)
.otherwise(() => {
throw Error(`Unsupported reference type: ${expr.target.$refText}`);
});
Expand Down
Loading
Loading