6
6
isLiteralExpr ,
7
7
ReferenceExpr ,
8
8
} from '@zenstackhq/language/ast' ;
9
- import { analyzePolicies , getIdFields , getLiteral } from '@zenstackhq/sdk' ;
9
+ import { analyzePolicies , getModelIdFields , getModelUniqueFields , getLiteral } from '@zenstackhq/sdk' ;
10
10
import { AstNode , DiagnosticInfo , getDocument , ValidationAcceptor } from 'langium' ;
11
11
import { IssueCodes , SCALAR_TYPES } from '../constants' ;
12
12
import { AstValidator } from '../types' ;
@@ -26,16 +26,29 @@ export default class DataModelValidator implements AstValidator<DataModel> {
26
26
27
27
private validateFields ( dm : DataModel , accept : ValidationAcceptor ) {
28
28
const idFields = dm . $resolvedFields . filter ( ( f ) => f . attributes . find ( ( attr ) => attr . decl . ref ?. name === '@id' ) ) ;
29
- const modelLevelIds = getIdFields ( dm ) ;
30
-
31
- if ( idFields . length === 0 && modelLevelIds . length === 0 ) {
29
+ const uniqueFields = dm . $resolvedFields . filter ( ( f ) =>
30
+ f . attributes . find ( ( attr ) => attr . decl . ref ?. name === '@unique' )
31
+ ) ;
32
+ const modelLevelIds = getModelIdFields ( dm ) ;
33
+ const modelUniqueFields = getModelUniqueFields ( dm ) ;
34
+
35
+ if (
36
+ idFields . length === 0 &&
37
+ modelLevelIds . length === 0 &&
38
+ uniqueFields . length === 0 &&
39
+ modelUniqueFields . length === 0
40
+ ) {
32
41
const { allows, denies, hasFieldValidation } = analyzePolicies ( dm ) ;
33
42
if ( allows . length > 0 || denies . length > 0 || hasFieldValidation ) {
34
43
// TODO: relax this requirement to require only @unique fields
35
44
// when access policies or field valdaition is used, require an @id field
36
- accept ( 'error' , 'Model must include a field with @id attribute or a model-level @@id attribute' , {
37
- node : dm ,
38
- } ) ;
45
+ accept (
46
+ 'error' ,
47
+ 'Model must include a field with @id or @unique attribute, or a model-level @@id or @@unique attribute to use access policies' ,
48
+ {
49
+ node : dm ,
50
+ }
51
+ ) ;
39
52
}
40
53
} else if ( idFields . length > 0 && modelLevelIds . length > 0 ) {
41
54
accept ( 'error' , 'Model cannot have both field-level @id and model-level @@id attributes' , {
0 commit comments