This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdatamodel-validator.ts
More file actions
481 lines (432 loc) · 18.9 KB
/
datamodel-validator.ts
File metadata and controls
481 lines (432 loc) · 18.9 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import { invariant } from '@zenstackhq/common-helpers';
import { AstUtils, type AstNode, type DiagnosticInfo, type ValidationAcceptor } from 'langium';
import { IssueCodes, SCALAR_TYPES } from '../constants';
import {
ArrayExpr,
DataField,
DataModel,
ReferenceExpr,
TypeDef,
isDataModel,
isEnum,
isStringLiteral,
isTypeDef,
} from '../generated/ast';
import {
getAllAttributes,
getAllFields,
getModelIdFields,
getModelUniqueFields,
getUniqueFields,
hasAttribute,
isDelegateModel,
} from '../utils';
import { validateAttributeApplication } from './attribute-application-validator';
import { validateDuplicatedDeclarations, type AstValidator } from './common';
/**
* Validates data model declarations.
*/
export default class DataModelValidator implements AstValidator<DataModel> {
validate(dm: DataModel, accept: ValidationAcceptor): void {
validateDuplicatedDeclarations(dm, getAllFields(dm), accept);
this.validateAttributes(dm, accept);
this.validateFields(dm, accept);
if (dm.mixins.length > 0) {
this.validateMixins(dm, accept);
}
this.validateInherits(dm, accept);
}
private validateFields(dm: DataModel, accept: ValidationAcceptor) {
const allFields = getAllFields(dm);
const idFields = allFields.filter((f) => f.attributes.find((attr) => attr.decl.ref?.name === '@id'));
const uniqueFields = allFields.filter((f) => f.attributes.find((attr) => attr.decl.ref?.name === '@unique'));
const modelLevelIds = getModelIdFields(dm);
const modelUniqueFields = getModelUniqueFields(dm);
if (
!dm.isView &&
idFields.length === 0 &&
modelLevelIds.length === 0 &&
uniqueFields.length === 0 &&
modelUniqueFields.length === 0
) {
accept(
'error',
'Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.',
{
node: dm,
},
);
} else if (idFields.length > 0 && modelLevelIds.length > 0) {
accept('error', 'Model cannot have both field-level @id and model-level @@id attributes', {
node: dm,
});
} else if (idFields.length > 1) {
accept('error', 'Model can include at most one field with @id attribute', {
node: dm,
});
} else {
const fieldsToCheck = idFields.length > 0 ? idFields : modelLevelIds;
fieldsToCheck.forEach((idField) => {
if (idField.type.optional) {
accept('error', 'Field with @id attribute must not be optional', { 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 });
}
});
}
dm.fields.forEach((field) => this.validateField(field, accept));
allFields
.filter((x) => isDataModel(x.type.reference?.ref))
.forEach((y) => {
this.validateRelationField(dm, y, accept);
});
}
private validateField(field: DataField, accept: ValidationAcceptor): void {
if (field.type.array && field.type.optional) {
accept('error', 'Optional lists are not supported. Use either `Type[]` or `Type?`', { node: field.type });
}
if (field.type.unsupported && !isStringLiteral(field.type.unsupported.value)) {
accept('error', 'Unsupported type argument must be a string literal', { node: field.type.unsupported });
}
field.attributes.forEach((attr) => validateAttributeApplication(attr, accept));
if (isTypeDef(field.type.reference?.ref)) {
if (!hasAttribute(field, '@json')) {
accept('error', 'Custom-typed field must have @json attribute', { node: field });
}
}
}
private validateAttributes(dm: DataModel, accept: ValidationAcceptor) {
getAllAttributes(dm).forEach((attr) => validateAttributeApplication(attr, accept, dm));
}
private parseRelation(field: DataField, accept?: ValidationAcceptor) {
const relAttr = field.attributes.find((attr) => attr.decl.ref?.name === '@relation');
let name: string | undefined;
let fields: ReferenceExpr[] | undefined;
let references: ReferenceExpr[] | undefined;
let valid = true;
if (!relAttr) {
return { attr: relAttr, name, fields, references, valid: true };
}
for (const arg of relAttr.args) {
if (!arg.name || arg.name === 'name') {
if (isStringLiteral(arg.value)) {
name = arg.value.value as string;
}
} else if (arg.name === 'fields') {
fields = (arg.value as ArrayExpr).items as ReferenceExpr[];
if (fields.length === 0) {
if (accept) {
accept('error', `"fields" value cannot be empty`, {
node: arg,
});
}
valid = false;
}
} else if (arg.name === 'references') {
references = (arg.value as ArrayExpr).items as ReferenceExpr[];
if (references.length === 0) {
if (accept) {
accept('error', `"references" value cannot be empty`, {
node: arg,
});
}
valid = false;
}
}
}
if (!fields && !references) {
return { attr: relAttr, name, fields, references, valid: true };
}
if (!fields || !references) {
if (accept) {
accept('error', `"fields" and "references" must be provided together`, { node: relAttr });
}
} else {
// validate "fields" and "references" typing consistency
if (fields.length !== references.length) {
if (accept) {
accept('error', `"references" and "fields" must have the same length`, { node: relAttr });
}
} else {
for (let i = 0; i < fields.length; i++) {
const fieldRef = fields[i];
if (!fieldRef) {
continue;
}
if (!field.type.optional && fieldRef.$resolvedType?.nullable) {
// if relation is not optional, then fk field must not be nullable
if (accept) {
accept(
'error',
`relation "${field.name}" is not optional, but field "${fieldRef.target.$refText}" is optional`,
{ node: fieldRef.target.ref! },
);
}
}
if (!fieldRef.$resolvedType) {
if (accept) {
accept('error', `field reference is unresolved`, {
node: fieldRef,
});
}
}
if (!references[i]?.$resolvedType) {
if (accept) {
accept('error', `field reference is unresolved`, {
node: references[i]!,
});
}
}
if (
fieldRef.$resolvedType?.decl !== references[i]?.$resolvedType?.decl ||
fieldRef.$resolvedType?.array !== references[i]?.$resolvedType?.array
) {
if (accept) {
accept('error', `values of "references" and "fields" must have the same type`, {
node: relAttr,
});
}
}
}
}
}
return { attr: relAttr, name, fields, references, valid };
}
private isSelfRelation(field: DataField) {
return field.type.reference?.ref === field.$container;
}
private validateRelationField(contextModel: DataModel, field: DataField, accept: ValidationAcceptor) {
const thisRelation = this.parseRelation(field, accept);
if (!thisRelation.valid) {
return;
}
if (this.isFieldInheritedFromDelegateModel(field, contextModel)) {
// relation fields inherited from delegate model don't need opposite relation
return;
}
if (this.isSelfRelation(field)) {
if (!thisRelation.name) {
accept('error', 'Self-relation field must have a name in @relation attribute', {
node: field,
});
return;
}
}
const oppositeModel = field.type.reference!.ref! as DataModel;
// Use name because the current document might be updated
let oppositeFields = getAllFields(oppositeModel, false).filter(
(f) =>
f !== field && // exclude self in case of self relation
f.type.reference?.ref?.name === contextModel.name,
);
oppositeFields = oppositeFields.filter((f) => {
const fieldRel = this.parseRelation(f);
return fieldRel.valid && fieldRel.name === thisRelation.name;
});
if (oppositeFields.length === 0) {
const info: DiagnosticInfo<AstNode, string> = {
node: field,
code: IssueCodes.MissingOppositeRelation,
};
info.property = 'name';
const container = field.$container;
const relationFieldDocUri = AstUtils.getDocument(container).textDocument.uri;
const relationDataModelName = container.name;
const data: MissingOppositeRelationData = {
relationFieldName: field.name,
relationDataModelName,
relationFieldDocUri,
dataModelName: contextModel.name,
};
info.data = data;
accept(
'error',
`The relation field "${field.name}" on model "${contextModel.name}" is missing an opposite relation field on model "${oppositeModel.name}"`,
info,
);
return;
} else if (oppositeFields.length > 1) {
oppositeFields
.filter((f) => f.$container !== contextModel)
.forEach((f) => {
if (this.isSelfRelation(f)) {
// self relations are partial
// https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations
} else {
accept(
'error',
`Fields ${oppositeFields.map((f) => '"' + f.name + '"').join(', ')} on model "${
oppositeModel.name
}" refer to the same relation to model "${field.$container.name}"`,
{ node: f },
);
}
});
return;
}
const oppositeField = oppositeFields[0]!;
const oppositeRelation = this.parseRelation(oppositeField);
let relationOwner: DataField;
if (field.type.array && oppositeField.type.array) {
// if both the field is array, then it's an implicit many-to-many relation,
// neither side should have fields/references
for (const r of [thisRelation, oppositeRelation]) {
if (r.fields?.length || r.references?.length) {
accept(
'error',
'Implicit many-to-many relation cannot have "fields" or "references" in @relation attribute',
{
node: r === thisRelation ? field : oppositeField,
},
);
}
}
} else {
if (thisRelation?.references?.length && thisRelation.fields?.length) {
if (oppositeRelation?.references || oppositeRelation?.fields) {
accept('error', '"fields" and "references" must be provided only on one side of relation field', {
node: oppositeField,
});
return;
} else {
relationOwner = oppositeField;
}
} else if (oppositeRelation?.references?.length && oppositeRelation.fields?.length) {
if (thisRelation?.references || thisRelation?.fields) {
accept('error', '"fields" and "references" must be provided only on one side of relation field', {
node: field,
});
return;
} else {
relationOwner = field;
}
} else {
// for non-M2M relations, one side must have fields/references
[field, oppositeField].forEach((f) => {
if (!this.isSelfRelation(f)) {
accept(
'error',
'Field for one side of relation must carry @relation attribute with both "fields" and "references"',
{ node: f },
);
}
});
return;
}
if (!relationOwner.type.array && !relationOwner.type.optional) {
accept('error', 'Relation field needs to be list or optional', {
node: relationOwner,
});
return;
}
if (relationOwner !== field && !relationOwner.type.array) {
// one-to-one relation requires defining side's reference field to be @unique
// e.g.:
// model User {
// id String @id @default(cuid())
// data UserData?
// }
// model UserData {
// id String @id @default(cuid())
// user User @relation(fields: [userId], references: [id])
// userId String
// }
//
// UserData.userId field needs to be @unique
const containingModel = field.$container as DataModel;
const uniqueFieldList = getUniqueFields(containingModel);
// field is defined in the abstract base model
if (containingModel !== contextModel) {
uniqueFieldList.push(...getUniqueFields(contextModel));
}
thisRelation.fields?.forEach((ref) => {
const refField = ref.target.ref as DataField;
if (refField) {
if (
refField.attributes.find(
(a) => a.decl.ref?.name === '@id' || a.decl.ref?.name === '@unique',
)
) {
return;
}
if (uniqueFieldList.some((list) => list.includes(refField))) {
return;
}
accept(
'error',
`Field "${refField.name}" on model "${containingModel.name}" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`,
{ node: refField },
);
}
});
}
}
}
// checks if the given field is inherited directly or indirectly from a delegate model
private isFieldInheritedFromDelegateModel(field: DataField, contextModel: DataModel) {
return field.$container !== contextModel && isDelegateModel(field.$container);
}
private validateInherits(model: DataModel, accept: ValidationAcceptor) {
if (!model.baseModel) {
return;
}
invariant(model.baseModel.ref, 'baseModel must be resolved');
// check if the base model is a delegate model
if (!isDelegateModel(model.baseModel.ref!)) {
accept('error', `Model ${model.baseModel.$refText} cannot be extended because it's not a delegate model`, {
node: model,
property: 'baseModel',
});
return;
}
// check for cyclic inheritance
const seen: DataModel[] = [];
const todo = [model.baseModel.ref];
while (todo.length > 0) {
const current = todo.shift()!;
if (seen.includes(current)) {
accept(
'error',
`Cyclic inheritance detected: ${seen.map((m) => m.name).join(' -> ')} -> ${current.name}`,
{
node: model,
},
);
return;
}
seen.push(current);
if (current.baseModel) {
invariant(current.baseModel.ref, 'baseModel must be resolved');
todo.push(current.baseModel.ref);
}
}
}
private validateMixins(dm: DataModel, accept: ValidationAcceptor) {
const seen: TypeDef[] = [];
const todo: TypeDef[] = dm.mixins.map((mixin) => mixin.ref!);
while (todo.length > 0) {
const current = todo.shift()!;
if (seen.includes(current)) {
accept('error', `Cyclic mixin detected: ${seen.map((m) => m.name).join(' -> ')} -> ${current.name}`, {
node: dm,
});
return;
}
seen.push(current);
todo.push(...current.mixins.map((mixin) => mixin.ref!));
}
}
}
export interface MissingOppositeRelationData {
relationDataModelName: string;
relationFieldName: string;
// it might be the abstract model in the imported document
relationFieldDocUri: string;
// the name of DataModel that the relation field belongs to.
// the document is the same with the error node.
dataModelName: string;
}