Skip to content

Commit

Permalink
Allows enum types for id fields (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ustice authored Feb 19, 2024
1 parent 0704f9d commit d7b75e9
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 204 deletions.
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

0 comments on commit d7b75e9

Please sign in to comment.