Skip to content

Allows enum types for id fields #1010

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

Merged
merged 3 commits into from
Feb 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isDataModel,
isStringLiteral,
ReferenceExpr,
isEnum,
} from '@zenstackhq/language/ast';
import { getLiteral, getModelIdFields, getModelUniqueFields } from '@zenstackhq/sdk';
import { AstNode, DiagnosticInfo, getDocument, ValidationAcceptor } from 'langium';
Expand Down Expand Up @@ -61,8 +62,13 @@ export default class DataModelValidator implements AstValidator<DataModel> {
if (idField.type.optional) {
accept('error', 'Field with @id attribute must not be optional', { node: idField });
}
if (idField.type.array || !idField.type.type || !SCALAR_TYPES.includes(idField.type.type)) {
accept('error', 'Field with @id attribute must be of scalar type', { node: idField });

const isArray = idField.type.array;
const isScalar = SCALAR_TYPES.includes(idField.type.type as typeof SCALAR_TYPES[number])
const isValidType = isScalar || isEnum(idField.type.reference?.ref)

if (isArray || !isValidType) {
accept('error', 'Field with @id attribute must be of scalar or enum type', { node: idField });
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/tests/schema/stdlib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Stdlib Tests', () => {
}`
);
}
throw new SchemaLoadingError(validationErrors.map((e) => e.message));
throw new SchemaLoadingError(validationErrors);
}
});
});
Loading