-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathvalidation.ts
More file actions
46 lines (41 loc) · 1.17 KB
/
validation.ts
File metadata and controls
46 lines (41 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {
isDataModel,
isTypeDef,
type DataModel,
type DataModelAttribute,
type DataModelFieldAttribute,
type TypeDef,
} from './ast';
export function isValidationAttribute(attr: DataModelAttribute | DataModelFieldAttribute) {
return attr.decl.ref?.attributes.some((attr) => attr.decl.$refText === '@@@validation');
}
/**
* Returns if the given model contains any data validation rules (both at the model
* level and at the field level).
*/
export function hasValidationAttributes(
decl: DataModel | TypeDef,
seen: Set<DataModel | TypeDef> = new Set()
): boolean {
if (seen.has(decl)) {
return false;
}
seen.add(decl);
if (isDataModel(decl)) {
if (decl.attributes.some((attr) => isValidationAttribute(attr))) {
return true;
}
}
if (
decl.fields.some((field) => {
if (isTypeDef(field.type.reference?.ref)) {
return hasValidationAttributes(field.type.reference?.ref, seen);
} else {
return field.attributes.some((attr) => isValidationAttribute(attr));
}
})
) {
return true;
}
return false;
}