Skip to content

Commit d7b75e9

Browse files
authored
Allows enum types for id fields (#1010)
1 parent 0704f9d commit d7b75e9

File tree

5 files changed

+286
-204
lines changed

5 files changed

+286
-204
lines changed

packages/schema/src/language-server/validator/datamodel-validator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isDataModel,
66
isStringLiteral,
77
ReferenceExpr,
8+
isEnum,
89
} from '@zenstackhq/language/ast';
910
import { getLiteral, getModelIdFields, getModelUniqueFields } from '@zenstackhq/sdk';
1011
import { AstNode, DiagnosticInfo, getDocument, ValidationAcceptor } from 'langium';
@@ -61,8 +62,13 @@ export default class DataModelValidator implements AstValidator<DataModel> {
6162
if (idField.type.optional) {
6263
accept('error', 'Field with @id attribute must not be optional', { node: idField });
6364
}
64-
if (idField.type.array || !idField.type.type || !SCALAR_TYPES.includes(idField.type.type)) {
65-
accept('error', 'Field with @id attribute must be of scalar type', { node: idField });
65+
66+
const isArray = idField.type.array;
67+
const isScalar = SCALAR_TYPES.includes(idField.type.type as typeof SCALAR_TYPES[number])
68+
const isValidType = isScalar || isEnum(idField.type.reference?.ref)
69+
70+
if (isArray || !isValidType) {
71+
accept('error', 'Field with @id attribute must be of scalar or enum type', { node: idField });
6672
}
6773
});
6874
}

packages/schema/tests/schema/stdlib.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Stdlib Tests', () => {
2424
}`
2525
);
2626
}
27-
throw new SchemaLoadingError(validationErrors.map((e) => e.message));
27+
throw new SchemaLoadingError(validationErrors);
2828
}
2929
});
3030
});

0 commit comments

Comments
 (0)