@prisma-next/sql-contract 0.3.0-dev.130 → 0.3.0-dev.132
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -9
- package/dist/factories.d.mts +11 -15
- package/dist/factories.d.mts.map +1 -1
- package/dist/factories.mjs +10 -13
- package/dist/factories.mjs.map +1 -1
- package/dist/{types-CB821Pqa.d.mts → types-D6K16_9R.d.mts} +4 -23
- package/dist/types-D6K16_9R.d.mts.map +1 -0
- package/dist/types-DRR5stkj.mjs.map +1 -1
- package/dist/types.d.mts +2 -2
- package/dist/validate.d.mts +1 -1
- package/dist/validate.d.mts.map +1 -1
- package/dist/validate.mjs +39 -270
- package/dist/validate.mjs.map +1 -1
- package/dist/{validators-CNxeypbZ.mjs → validators-_1sYf-29.mjs} +4 -23
- package/dist/validators-_1sYf-29.mjs.map +1 -0
- package/dist/validators.d.mts +2 -9
- package/dist/validators.d.mts.map +1 -1
- package/dist/validators.mjs +1 -1
- package/package.json +3 -3
- package/src/construct.ts +2 -167
- package/src/exports/types.ts +0 -4
- package/src/factories.ts +24 -40
- package/src/types.ts +3 -28
- package/src/validate.ts +57 -286
- package/src/validators.ts +5 -26
- package/dist/types-CB821Pqa.d.mts.map +0 -1
- package/dist/validators-CNxeypbZ.mjs.map +0 -1
package/src/validate.ts
CHANGED
|
@@ -14,6 +14,38 @@ function extractDomainShape(contract: SqlContract<SqlStorage>): DomainContractSh
|
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
function validateModelStorageReferences(contract: SqlContract<SqlStorage>): void {
|
|
18
|
+
const models = contract.models as Record<
|
|
19
|
+
string,
|
|
20
|
+
{ storage?: { table?: string; fields?: Record<string, { column?: string }> } }
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
24
|
+
const storageTable = model.storage?.table;
|
|
25
|
+
if (!storageTable) continue;
|
|
26
|
+
|
|
27
|
+
const table = contract.storage.tables[storageTable] as
|
|
28
|
+
| (typeof contract.storage.tables)[string]
|
|
29
|
+
| undefined;
|
|
30
|
+
if (!table) {
|
|
31
|
+
throw new Error(`Model "${modelName}" references non-existent table "${storageTable}"`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const storageFields = model.storage?.fields;
|
|
35
|
+
if (!storageFields) continue;
|
|
36
|
+
|
|
37
|
+
const columnNames = new Set(Object.keys(table.columns));
|
|
38
|
+
for (const [fieldName, field] of Object.entries(storageFields)) {
|
|
39
|
+
const column = field.column;
|
|
40
|
+
if (column && !columnNames.has(column)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Model "${modelName}" field "${fieldName}" references non-existent column "${column}" in table "${storageTable}"`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
17
49
|
function validateContractLogic(contract: SqlContract<SqlStorage>): void {
|
|
18
50
|
const tableNames = new Set(Object.keys(contract.storage.tables));
|
|
19
51
|
|
|
@@ -169,9 +201,6 @@ export function decodeContractDefaults<T extends SqlContract<SqlStorage>>(contra
|
|
|
169
201
|
return contract;
|
|
170
202
|
}
|
|
171
203
|
|
|
172
|
-
// The spread widens to SqlContract<SqlStorage>, but this transformation only
|
|
173
|
-
// decodes tagged bigint defaults for bigint-like columns and preserves all
|
|
174
|
-
// other properties of T.
|
|
175
204
|
return {
|
|
176
205
|
...contract,
|
|
177
206
|
storage: {
|
|
@@ -229,242 +258,17 @@ function normalizeStorage(contractObj: Record<string, unknown>): Record<string,
|
|
|
229
258
|
return { ...storage, tables: normalizedTables };
|
|
230
259
|
}
|
|
231
260
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
function detectFormat(models: Record<string, RawModel>): 'old' | 'new' {
|
|
238
|
-
for (const model of Object.values(models)) {
|
|
239
|
-
const fields = model['fields'] as Record<string, RawField> | undefined;
|
|
240
|
-
if (!fields) continue;
|
|
241
|
-
for (const field of Object.values(fields)) {
|
|
242
|
-
if ('column' in field) return 'old';
|
|
243
|
-
if ('codecId' in field) return 'new';
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return 'old';
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function buildColumnToFieldMap(
|
|
250
|
-
fields: Record<string, RawField>,
|
|
251
|
-
modelName: string,
|
|
252
|
-
): Record<string, string> {
|
|
253
|
-
const map: Record<string, string> = {};
|
|
254
|
-
for (const [fieldName, field] of Object.entries(fields)) {
|
|
255
|
-
const col = field['column'] as string | undefined;
|
|
256
|
-
if (!col) continue;
|
|
257
|
-
if (Object.hasOwn(map, col)) {
|
|
258
|
-
throw new Error(
|
|
259
|
-
`Model "${modelName}" has duplicate column mapping: fields "${map[col]}" and "${fieldName}" both map to column "${col}"`,
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
map[col] = fieldName;
|
|
263
|
-
}
|
|
264
|
-
return map;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function enrichOldFormatModels(
|
|
268
|
-
models: Record<string, RawModel>,
|
|
269
|
-
storageObj: RawStorageObj,
|
|
270
|
-
topRelations: Record<string, Record<string, RawRelation>>,
|
|
271
|
-
): { enrichedModels: Record<string, RawModel>; roots: Record<string, string> } {
|
|
272
|
-
const roots: Record<string, string> = {};
|
|
273
|
-
const tableToModel: Record<string, string> = {};
|
|
274
|
-
|
|
275
|
-
for (const [modelName, model] of Object.entries(models)) {
|
|
276
|
-
const modelStorage = model['storage'] as Record<string, unknown> | undefined;
|
|
277
|
-
const tableName = modelStorage?.['table'] as string | undefined;
|
|
278
|
-
if (tableName) {
|
|
279
|
-
if (!model['owner']) {
|
|
280
|
-
roots[modelName] = modelName;
|
|
281
|
-
}
|
|
282
|
-
tableToModel[tableName] = modelName;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const enrichedModels: Record<string, RawModel> = {};
|
|
287
|
-
|
|
261
|
+
function normalizeModels(
|
|
262
|
+
models: Record<string, Record<string, unknown>>,
|
|
263
|
+
): Record<string, Record<string, unknown>> {
|
|
264
|
+
const normalized: Record<string, Record<string, unknown>> = {};
|
|
288
265
|
for (const [modelName, model] of Object.entries(models)) {
|
|
289
|
-
|
|
290
|
-
const modelStorage = model['storage'] as Record<string, unknown> | undefined;
|
|
291
|
-
const tableName = modelStorage?.['table'] as string | undefined;
|
|
292
|
-
const storageTable = tableName
|
|
293
|
-
? (storageObj.tables[tableName] as Record<string, unknown> | undefined)
|
|
294
|
-
: undefined;
|
|
295
|
-
const storageColumns = (storageTable?.['columns'] ?? {}) as Record<
|
|
296
|
-
string,
|
|
297
|
-
Record<string, unknown>
|
|
298
|
-
>;
|
|
299
|
-
|
|
300
|
-
const enrichedFields: Record<string, RawField> = {};
|
|
301
|
-
const modelStorageFields: Record<string, { column: string }> = {};
|
|
302
|
-
|
|
303
|
-
const hasStorageColumns = Object.keys(storageColumns).length > 0;
|
|
304
|
-
for (const [fieldName, field] of Object.entries(fields)) {
|
|
305
|
-
const colName = field['column'] as string;
|
|
306
|
-
const storageCol = storageColumns[colName];
|
|
307
|
-
if (!storageCol && hasStorageColumns && colName) {
|
|
308
|
-
throw new Error(
|
|
309
|
-
`Model "${modelName}" field "${fieldName}" references non-existent column "${colName}" in table "${tableName}"`,
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
enrichedFields[fieldName] = {
|
|
313
|
-
...field,
|
|
314
|
-
nullable: storageCol?.['nullable'] ?? false,
|
|
315
|
-
codecId: storageCol?.['codecId'] ?? '',
|
|
316
|
-
};
|
|
317
|
-
modelStorageFields[fieldName] = { column: colName };
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const enrichedStorage = {
|
|
321
|
-
...(modelStorage ?? {}),
|
|
322
|
-
fields: modelStorageFields,
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
enrichedModels[modelName] = {
|
|
266
|
+
normalized[modelName] = {
|
|
326
267
|
...model,
|
|
327
|
-
fields: enrichedFields,
|
|
328
|
-
storage: enrichedStorage,
|
|
329
268
|
relations: model['relations'] ?? {},
|
|
330
269
|
};
|
|
331
270
|
}
|
|
332
|
-
|
|
333
|
-
for (const [tableName, tableRels] of Object.entries(topRelations)) {
|
|
334
|
-
const modelName = tableToModel[tableName];
|
|
335
|
-
if (!modelName) continue;
|
|
336
|
-
const existingModel = enrichedModels[modelName];
|
|
337
|
-
if (!existingModel) continue;
|
|
338
|
-
|
|
339
|
-
const existingRels = (existingModel['relations'] ?? {}) as Record<string, unknown>;
|
|
340
|
-
const targetColumnToField: Record<string, Record<string, string>> = {};
|
|
341
|
-
|
|
342
|
-
const modelRelations: Record<string, unknown> = { ...existingRels };
|
|
343
|
-
for (const [relName, rel] of Object.entries(tableRels)) {
|
|
344
|
-
const on = rel['on'] as { childCols?: string[]; parentCols?: string[] } | undefined;
|
|
345
|
-
const parentCols = on?.['parentCols'] ?? [];
|
|
346
|
-
const childCols = on?.['childCols'] ?? [];
|
|
347
|
-
|
|
348
|
-
const toModel = rel['to'] as string;
|
|
349
|
-
const sourceFields = (existingModel['fields'] ?? {}) as Record<string, RawField>;
|
|
350
|
-
const sourceColToField = buildColumnToFieldMap(sourceFields, modelName);
|
|
351
|
-
|
|
352
|
-
if (!targetColumnToField[toModel]) {
|
|
353
|
-
const targetModelObj = enrichedModels[toModel];
|
|
354
|
-
if (targetModelObj) {
|
|
355
|
-
targetColumnToField[toModel] = buildColumnToFieldMap(
|
|
356
|
-
(targetModelObj['fields'] ?? {}) as Record<string, RawField>,
|
|
357
|
-
toModel,
|
|
358
|
-
);
|
|
359
|
-
} else {
|
|
360
|
-
targetColumnToField[toModel] = {};
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
const targetColToField = targetColumnToField[toModel] ?? {};
|
|
364
|
-
|
|
365
|
-
// Old format: parentCols = columns on FK-holding table (local), childCols = columns on referenced table (target)
|
|
366
|
-
const localFields = parentCols.map((c: string) => sourceColToField[c] ?? c);
|
|
367
|
-
const targetFields = childCols.map((c: string) => targetColToField[c] ?? c);
|
|
368
|
-
|
|
369
|
-
modelRelations[relName] = {
|
|
370
|
-
to: toModel,
|
|
371
|
-
cardinality: rel['cardinality'],
|
|
372
|
-
on: { localFields, targetFields },
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
enrichedModels[modelName] = {
|
|
377
|
-
...existingModel,
|
|
378
|
-
relations: modelRelations,
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
return { enrichedModels, roots };
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
function enrichNewFormatModels(models: Record<string, RawModel>): {
|
|
386
|
-
enrichedModels: Record<string, RawModel>;
|
|
387
|
-
topRelations: Record<string, Record<string, unknown>>;
|
|
388
|
-
} {
|
|
389
|
-
const enrichedModels: Record<string, RawModel> = {};
|
|
390
|
-
const topRelations: Record<string, Record<string, unknown>> = {};
|
|
391
|
-
const modelToTable: Record<string, string> = {};
|
|
392
|
-
|
|
393
|
-
for (const [modelName, model] of Object.entries(models)) {
|
|
394
|
-
const modelStorage = model['storage'] as Record<string, unknown> | undefined;
|
|
395
|
-
const tableName = modelStorage?.['table'] as string | undefined;
|
|
396
|
-
if (tableName) modelToTable[modelName] = tableName;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
for (const [modelName, model] of Object.entries(models)) {
|
|
400
|
-
const fields = (model['fields'] ?? {}) as Record<string, RawField>;
|
|
401
|
-
const modelStorage = model['storage'] as Record<string, unknown> | undefined;
|
|
402
|
-
const storageFields = (modelStorage?.['fields'] ?? {}) as Record<
|
|
403
|
-
string,
|
|
404
|
-
Record<string, unknown>
|
|
405
|
-
>;
|
|
406
|
-
|
|
407
|
-
const enrichedFields: Record<string, RawField> = {};
|
|
408
|
-
for (const [fieldName, field] of Object.entries(fields)) {
|
|
409
|
-
const sfEntry = storageFields[fieldName];
|
|
410
|
-
const column = sfEntry?.['column'] as string | undefined;
|
|
411
|
-
enrichedFields[fieldName] = column ? { ...field, column } : { ...field };
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
enrichedModels[modelName] = {
|
|
415
|
-
...model,
|
|
416
|
-
fields: enrichedFields,
|
|
417
|
-
relations: model['relations'] ?? {},
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
const modelRels = (model['relations'] ?? {}) as Record<string, RawRelation>;
|
|
421
|
-
const tableName = modelToTable[modelName];
|
|
422
|
-
if (!tableName) continue;
|
|
423
|
-
|
|
424
|
-
for (const [relName, rel] of Object.entries(modelRels)) {
|
|
425
|
-
const on = rel['on'] as { localFields?: string[]; targetFields?: string[] } | undefined;
|
|
426
|
-
if (!on) continue;
|
|
427
|
-
const toModel = rel['to'] as string;
|
|
428
|
-
const toTable = modelToTable[toModel];
|
|
429
|
-
if (!toTable) continue;
|
|
430
|
-
|
|
431
|
-
const sourceFields = enrichedFields;
|
|
432
|
-
const targetModelObj = models[toModel];
|
|
433
|
-
const targetFields = (targetModelObj?.['fields'] ?? {}) as Record<string, RawField>;
|
|
434
|
-
const targetStorageObj = targetModelObj?.['storage'] as Record<string, unknown> | undefined;
|
|
435
|
-
const targetStorageFields = (targetStorageObj?.['fields'] ?? {}) as Record<
|
|
436
|
-
string,
|
|
437
|
-
Record<string, unknown>
|
|
438
|
-
>;
|
|
439
|
-
|
|
440
|
-
const parentCols = (on.localFields ?? []).map((f: string) => {
|
|
441
|
-
const sf = storageFields[f];
|
|
442
|
-
return (
|
|
443
|
-
(sf?.['column'] as string | undefined) ??
|
|
444
|
-
(sourceFields[f]?.['column'] as string | undefined) ??
|
|
445
|
-
f
|
|
446
|
-
);
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
const childCols = (on.targetFields ?? []).map((f: string) => {
|
|
450
|
-
const tsf = targetStorageFields[f];
|
|
451
|
-
return (
|
|
452
|
-
(tsf?.['column'] as string | undefined) ??
|
|
453
|
-
(targetFields[f]?.['column'] as string | undefined) ??
|
|
454
|
-
f
|
|
455
|
-
);
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
if (!topRelations[tableName]) topRelations[tableName] = {};
|
|
459
|
-
topRelations[tableName][relName] = {
|
|
460
|
-
to: toModel,
|
|
461
|
-
cardinality: rel['cardinality'],
|
|
462
|
-
on: { parentCols, childCols },
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
return { enrichedModels, topRelations };
|
|
271
|
+
return normalized;
|
|
468
272
|
}
|
|
469
273
|
|
|
470
274
|
export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
@@ -476,66 +280,31 @@ export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
|
476
280
|
const normalizedStorage = normalizeStorage(contractObj);
|
|
477
281
|
|
|
478
282
|
const rawModels = contractObj['models'];
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
models: rawModels ?? {},
|
|
484
|
-
relations: contractObj['relations'] ?? {},
|
|
485
|
-
storage: normalizedStorage,
|
|
486
|
-
extensionPacks: contractObj['extensionPacks'] ?? {},
|
|
487
|
-
capabilities: contractObj['capabilities'] ?? {},
|
|
488
|
-
meta: contractObj['meta'] ?? {},
|
|
489
|
-
sources: contractObj['sources'] ?? {},
|
|
490
|
-
} as SqlContract<SqlStorage>;
|
|
491
|
-
}
|
|
283
|
+
const models =
|
|
284
|
+
rawModels && typeof rawModels === 'object' && rawModels !== null
|
|
285
|
+
? normalizeModels(rawModels as Record<string, Record<string, unknown>>)
|
|
286
|
+
: (rawModels ?? {});
|
|
492
287
|
|
|
493
|
-
const
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
let normalizedModels: Record<string, RawModel>;
|
|
497
|
-
let roots: Record<string, string>;
|
|
498
|
-
let topRelations: Record<string, Record<string, unknown>>;
|
|
499
|
-
|
|
500
|
-
if (format === 'new') {
|
|
501
|
-
const result = enrichNewFormatModels(modelsObj);
|
|
502
|
-
normalizedModels = result.enrichedModels;
|
|
503
|
-
topRelations = {
|
|
504
|
-
...((contractObj['relations'] ?? {}) as Record<string, Record<string, unknown>>),
|
|
505
|
-
...result.topRelations,
|
|
506
|
-
};
|
|
507
|
-
roots = (contractObj['roots'] as Record<string, string>) ?? {};
|
|
508
|
-
} else {
|
|
509
|
-
const rawStorageObj =
|
|
510
|
-
normalizedStorage && typeof normalizedStorage === 'object'
|
|
511
|
-
? (normalizedStorage as Record<string, unknown>)
|
|
512
|
-
: {};
|
|
513
|
-
const storageObj = {
|
|
514
|
-
tables: ((rawStorageObj as Record<string, unknown>)['tables'] ?? {}) as Record<
|
|
515
|
-
string,
|
|
516
|
-
Record<string, unknown>
|
|
517
|
-
>,
|
|
518
|
-
};
|
|
519
|
-
const existingRelations = (contractObj['relations'] ?? {}) as Record<
|
|
520
|
-
string,
|
|
521
|
-
Record<string, RawRelation>
|
|
522
|
-
>;
|
|
523
|
-
const result = enrichOldFormatModels(modelsObj, storageObj, existingRelations);
|
|
524
|
-
normalizedModels = result.enrichedModels;
|
|
525
|
-
roots = result.roots;
|
|
526
|
-
topRelations = existingRelations;
|
|
527
|
-
}
|
|
288
|
+
const pick = (key: string) =>
|
|
289
|
+
key in contractObj && contractObj[key] !== undefined ? { [key]: contractObj[key] } : {};
|
|
528
290
|
|
|
529
291
|
return {
|
|
530
|
-
...
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
292
|
+
...pick('schemaVersion'),
|
|
293
|
+
target: contractObj['target'],
|
|
294
|
+
targetFamily: contractObj['targetFamily'],
|
|
295
|
+
...pick('coreHash'),
|
|
296
|
+
storageHash: contractObj['storageHash'],
|
|
297
|
+
...pick('executionHash'),
|
|
298
|
+
...pick('profileHash'),
|
|
299
|
+
...pick('_generated'),
|
|
300
|
+
roots: contractObj['roots'] ?? {},
|
|
301
|
+
models,
|
|
534
302
|
storage: normalizedStorage,
|
|
535
303
|
extensionPacks: contractObj['extensionPacks'] ?? {},
|
|
536
304
|
capabilities: contractObj['capabilities'] ?? {},
|
|
537
305
|
meta: contractObj['meta'] ?? {},
|
|
538
306
|
sources: contractObj['sources'] ?? {},
|
|
307
|
+
...pick('execution'),
|
|
539
308
|
} as SqlContract<SqlStorage>;
|
|
540
309
|
}
|
|
541
310
|
|
|
@@ -550,6 +319,8 @@ export function validateContract<TContract extends SqlContract<SqlStorage>>(
|
|
|
550
319
|
|
|
551
320
|
validateContractLogic(structurallyValid);
|
|
552
321
|
|
|
322
|
+
validateModelStorageReferences(structurallyValid);
|
|
323
|
+
|
|
553
324
|
const semanticErrors = validateStorageSemantics(structurallyValid.storage);
|
|
554
325
|
if (semanticErrors.length > 0) {
|
|
555
326
|
throw new Error(`Contract semantic validation failed: ${semanticErrors.join('; ')}`);
|
package/src/validators.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { type } from 'arktype';
|
|
|
2
2
|
import type {
|
|
3
3
|
ForeignKey,
|
|
4
4
|
ForeignKeyReferences,
|
|
5
|
-
ModelDefinition,
|
|
6
5
|
PrimaryKey,
|
|
7
6
|
ReferentialAction,
|
|
8
7
|
SqlContract,
|
|
@@ -134,7 +133,6 @@ const StorageSchema = type({
|
|
|
134
133
|
});
|
|
135
134
|
|
|
136
135
|
const ModelFieldSchema = type({
|
|
137
|
-
'column?': 'string',
|
|
138
136
|
'nullable?': 'boolean',
|
|
139
137
|
'codecId?': 'string',
|
|
140
138
|
});
|
|
@@ -147,29 +145,19 @@ const ModelStorageFieldSchema = type({
|
|
|
147
145
|
|
|
148
146
|
const ModelStorageSchema = type({
|
|
149
147
|
table: 'string',
|
|
150
|
-
|
|
148
|
+
fields: type({ '[string]': ModelStorageFieldSchema }),
|
|
151
149
|
});
|
|
152
150
|
|
|
153
151
|
const ModelSchema = type({
|
|
154
152
|
storage: ModelStorageSchema,
|
|
155
|
-
fields: type({ '[string]': ModelFieldSchema }),
|
|
156
|
-
relations: type({ '[string]': 'unknown' }),
|
|
153
|
+
'fields?': type({ '[string]': ModelFieldSchema }),
|
|
154
|
+
'relations?': type({ '[string]': 'unknown' }),
|
|
157
155
|
'discriminator?': 'unknown',
|
|
158
156
|
'variants?': 'unknown',
|
|
159
157
|
'base?': 'string',
|
|
160
158
|
'owner?': 'string',
|
|
161
159
|
});
|
|
162
160
|
|
|
163
|
-
const MappingsSchema = type({
|
|
164
|
-
'+': 'reject',
|
|
165
|
-
'modelToTable?': 'null | Record<string, string>',
|
|
166
|
-
'tableToModel?': 'null | Record<string, string>',
|
|
167
|
-
'fieldToColumn?': 'null | Record<string, Record<string, string>>',
|
|
168
|
-
'columnToField?': 'null | Record<string, Record<string, string>>',
|
|
169
|
-
'codecTypes?': 'null | Record<string, unknown>',
|
|
170
|
-
'operationTypes?': 'null | Record<string, Record<string, unknown>>',
|
|
171
|
-
});
|
|
172
|
-
|
|
173
161
|
const ContractMetaSchema = type({
|
|
174
162
|
'[string]': 'unknown',
|
|
175
163
|
});
|
|
@@ -188,9 +176,7 @@ const SqlContractSchema = type({
|
|
|
188
176
|
'extensionPacks?': 'Record<string, unknown>',
|
|
189
177
|
'meta?': ContractMetaSchema,
|
|
190
178
|
'sources?': 'Record<string, unknown>',
|
|
191
|
-
'relations?': type({ '[string]': 'unknown' }),
|
|
192
179
|
'roots?': 'Record<string, string>',
|
|
193
|
-
'mappings?': MappingsSchema,
|
|
194
180
|
models: type({ '[string]': ModelSchema }),
|
|
195
181
|
storage: StorageSchema,
|
|
196
182
|
'execution?': ExecutionSchema,
|
|
@@ -219,20 +205,13 @@ export function validateStorage(value: unknown): SqlStorage {
|
|
|
219
205
|
return result as SqlStorage;
|
|
220
206
|
}
|
|
221
207
|
|
|
222
|
-
|
|
223
|
-
* Validates the structural shape of ModelDefinition using Arktype.
|
|
224
|
-
*
|
|
225
|
-
* @param value - The model value to validate
|
|
226
|
-
* @returns The validated model if structure is valid
|
|
227
|
-
* @throws Error if the model structure is invalid
|
|
228
|
-
*/
|
|
229
|
-
export function validateModel(value: unknown): ModelDefinition {
|
|
208
|
+
export function validateModel(value: unknown): unknown {
|
|
230
209
|
const result = ModelSchema(value);
|
|
231
210
|
if (result instanceof type.errors) {
|
|
232
211
|
const messages = result.map((p: { message: string }) => p.message).join('; ');
|
|
233
212
|
throw new Error(`Model validation failed: ${messages}`);
|
|
234
213
|
}
|
|
235
|
-
return result
|
|
214
|
+
return result;
|
|
236
215
|
}
|
|
237
216
|
|
|
238
217
|
/**
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-CB821Pqa.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;AAiBA;AAsBA;AAKA;AAKA;AAeA;AAKY,KApDA,aAAA,GAoDiB;EAEjB,SAAA,UAAA,EAAiB,MAAA;EAMjB,SAAA,OAAU,EAAA,MAAA;EAEC,SAAA,QAAA,EAAA,OAAA;EAED;;;AAQtB;;EACoB,SAAA,UAAA,CAAA,EAhEI,MAgEJ,CAAA,MAAA,EAAA,OAAA,CAAA;EACI;;;;EAEJ,SAAA,OAAA,CAAA,EAAA,MAAA;EACkB;;;AAatC;EAMY,SAAA,OAAU,CAAA,EA7ED,aA6EC;CACY;AAAf,KA3EP,UAAA,GA2EO;EAKe,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EAAf,SAAA,IAAA,CAAA,EAAA,MAAA;CAAM;AAGb,KA9EA,gBAAA,GA8EU;EAIV,SAAA,OAAY,EAAA,SAAA,MAAA,EAAA;EAIZ,SAAA,IAAA,CAAA,EAAA,MAAe;CACP;AACc,KAnFtB,KAAA,GAmFsB;EAAf,SAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EACG,SAAA,IAAA,CAAA,EAAA,MAAA;EAAM;AAI5B;AAMA;AAKA;AAMA;EAC0B,SAAA,KAAA,CAAA,EAAA,MAAA;EACA;;;EAEgB,SAAA,MAAA,CAAA,EAjGtB,MAiGsB,CAAA,MAAA,EAAA,OAAA,CAAA;CAAf;AAAM,KA9FrB,oBAAA,GA8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AA,CAAA;AAEJ,KA3DA,gBA2DqB,CAAA,CAAA,CAAA,GAAA,CA3DE,CA2DF,CAAA,SAAA,CAAA,KAAA,CAAA,GA1D7B,MA0D6B,CAAA,MAAA,EAAA,KAAA,CAAA,GAzD7B,CAyD6B,SAAA;EAA0B,SAAA,cAAA,EAAA,KAAA,EAAA;CACjC,GAAA,CAAA,SAzDV,MAyDU,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,CAAA,GAvDlB,MAuDkB,CAAA,MAAA,EAAA,KAAA,CAAA,GAtDpB,MAsDoB,CAAA,MAAA,EAAA,KAAA,CAAA;AAAtB,KApDQ,uBAAA,GAoDR;EACiB,SAAA,IAAA,EAAA,SAAA;IAAjB,SAAA,OAAA,EAAA,MAAA;IAAgB,SAAA,QAAA,EAAA,OAAA;;;;;;;KAhDR,iCAAiC,eAAe,4BAA4B;KAE5E,uBAAA,GAA0B,eAAe;KAEzC,4BAA4B,qBACpC,wBACA;;cACY,8BAER,wBACF;KAEM,kBAAA;KAEA,6CAA6C,6BACxC,sBAAsB;KAG3B,sBACA,aAAa,sBACb,0BAA0B,mCAC1B,0BAA0B,qCACxB,cAAc,kCACL,0BAA0B,gDACxB,4BAA4B,gDAC9B,0BAA0B,2BAC7C,aAAa,cAAc,gBAAgB,cAAc;;oBAEzC;sBACE;qBACD;uBACE;;KAGX,iCAAiC,iCAAiC,IAC1E,YAAY,EAAE,2BAA2B;KAGjC,uBAAuB,aAAa,4BAA4B;KAChE,2BAA2B,iBAAiB,4BAA4B;KACxE,gCAAgC,sBAAsB,4BAA4B;KAElF,2CAA2C,6BACnD,kBAAkB,aAClB,aAAa;KAEL,+CAA+C,6BACvD,sBAAsB,aACtB,iBAAiB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validators-CNxeypbZ.mjs","names":["errors: string[]"],"sources":["../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n ModelDefinition,\n PrimaryKey,\n ReferentialAction,\n SqlContract,\n SqlStorage,\n StorageTypeInstance,\n UniqueConstraint,\n} from './types';\n\ntype ColumnDefaultLiteral = {\n readonly kind: 'literal';\n readonly value: string | number | boolean | Record<string, unknown> | unknown[] | null;\n};\ntype ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };\nconst literalKindSchema = type(\"'literal'\");\nconst functionKindSchema = type(\"'function'\");\nconst generatorKindSchema = type(\"'generator'\");\nconst generatorIdSchema = type('string').narrow((value, ctx) => {\n return /^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(value) ? true : ctx.mustBe('a flat generator id');\n});\n\nexport const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({\n kind: literalKindSchema,\n value: 'string | number | boolean | null | unknown[] | Record<string, unknown>',\n});\n\nexport const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({\n kind: functionKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);\n\nconst ExecutionMutationDefaultValueSchema = type({\n '+': 'reject',\n kind: generatorKindSchema,\n id: generatorIdSchema,\n 'params?': 'Record<string, unknown>',\n});\n\nconst ExecutionMutationDefaultSchema = type({\n '+': 'reject',\n ref: {\n '+': 'reject',\n table: 'string',\n column: 'string',\n },\n 'onCreate?': ExecutionMutationDefaultValueSchema,\n 'onUpdate?': ExecutionMutationDefaultValueSchema,\n});\n\nconst ExecutionSchema = type({\n '+': 'reject',\n mutations: {\n '+': 'reject',\n defaults: ExecutionMutationDefaultSchema.array().readonly(),\n },\n});\n\nconst StorageColumnSchema = type({\n '+': 'reject',\n nativeType: 'string',\n codecId: 'string',\n nullable: 'boolean',\n 'typeParams?': 'Record<string, unknown>',\n 'typeRef?': 'string',\n 'default?': ColumnDefaultSchema,\n}).narrow((col, ctx) => {\n if (col.typeParams !== undefined && col.typeRef !== undefined) {\n return ctx.mustBe('a column with either typeParams or typeRef, not both');\n }\n return true;\n});\n\nconst StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({\n codecId: 'string',\n nativeType: 'string',\n typeParams: 'Record<string, unknown>',\n});\n\nconst PrimaryKeySchema = type.declare<PrimaryKey>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst UniqueConstraintSchema = type.declare<UniqueConstraint>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nexport const IndexSchema = type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n 'using?': 'string',\n 'config?': 'Record<string, unknown>',\n});\n\nexport const ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({\n table: 'string',\n columns: type.string.array().readonly(),\n});\n\nexport const ReferentialActionSchema = type\n .declare<ReferentialAction>()\n .type(\"'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault'\");\n\nexport const ForeignKeySchema = type.declare<ForeignKey>().type({\n columns: type.string.array().readonly(),\n references: ForeignKeyReferencesSchema,\n 'name?': 'string',\n 'onDelete?': ReferentialActionSchema,\n 'onUpdate?': ReferentialActionSchema,\n constraint: 'boolean',\n index: 'boolean',\n});\n\nconst StorageTableSchema = type({\n '+': 'reject',\n columns: type({ '[string]': StorageColumnSchema }),\n 'primaryKey?': PrimaryKeySchema,\n uniques: UniqueConstraintSchema.array().readonly(),\n indexes: IndexSchema.array().readonly(),\n foreignKeys: ForeignKeySchema.array().readonly(),\n});\n\nconst StorageSchema = type({\n '+': 'reject',\n tables: type({ '[string]': StorageTableSchema }),\n 'types?': type({ '[string]': StorageTypeInstanceSchema }),\n});\n\nconst ModelFieldSchema = type({\n 'column?': 'string',\n 'nullable?': 'boolean',\n 'codecId?': 'string',\n});\n\nconst ModelStorageFieldSchema = type({\n column: 'string',\n 'codecId?': 'string',\n 'nullable?': 'boolean',\n});\n\nconst ModelStorageSchema = type({\n table: 'string',\n 'fields?': type({ '[string]': ModelStorageFieldSchema }),\n});\n\nconst ModelSchema = type({\n storage: ModelStorageSchema,\n fields: type({ '[string]': ModelFieldSchema }),\n relations: type({ '[string]': 'unknown' }),\n 'discriminator?': 'unknown',\n 'variants?': 'unknown',\n 'base?': 'string',\n 'owner?': 'string',\n});\n\nconst MappingsSchema = type({\n '+': 'reject',\n 'modelToTable?': 'null | Record<string, string>',\n 'tableToModel?': 'null | Record<string, string>',\n 'fieldToColumn?': 'null | Record<string, Record<string, string>>',\n 'columnToField?': 'null | Record<string, Record<string, string>>',\n 'codecTypes?': 'null | Record<string, unknown>',\n 'operationTypes?': 'null | Record<string, Record<string, unknown>>',\n});\n\nconst ContractMetaSchema = type({\n '[string]': 'unknown',\n});\n\nconst SqlContractSchema = type({\n '+': 'reject',\n 'schemaVersion?': \"'1'\",\n target: 'string',\n targetFamily: \"'sql'\",\n 'coreHash?': 'string',\n storageHash: 'string',\n 'executionHash?': 'string',\n 'profileHash?': 'string',\n '_generated?': 'Record<string, unknown>',\n 'capabilities?': 'Record<string, Record<string, boolean>>',\n 'extensionPacks?': 'Record<string, unknown>',\n 'meta?': ContractMetaSchema,\n 'sources?': 'Record<string, unknown>',\n 'relations?': type({ '[string]': 'unknown' }),\n 'roots?': 'Record<string, string>',\n 'mappings?': MappingsSchema,\n models: type({ '[string]': ModelSchema }),\n storage: StorageSchema,\n 'execution?': ExecutionSchema,\n});\n\n// NOTE: StorageColumnSchema, StorageTableSchema, and StorageSchema use bare type()\n// instead of type.declare<T>().type() because the ColumnDefault union's value field\n// includes bigint | Date (runtime-only types after decoding) which cannot be expressed\n// in Arktype's JSON validation DSL. The `as SqlStorage` cast in validateStorage() bridges\n// the gap between the JSON-safe Arktype output and the runtime TypeScript type.\n// See decodeContractDefaults() in validate.ts for the decoding step.\n\n/**\n * Validates the structural shape of SqlStorage using Arktype.\n *\n * @param value - The storage value to validate\n * @returns The validated storage if structure is valid\n * @throws Error if the storage structure is invalid\n */\nexport function validateStorage(value: unknown): SqlStorage {\n const result = StorageSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Storage validation failed: ${messages}`);\n }\n return result as SqlStorage;\n}\n\n/**\n * Validates the structural shape of ModelDefinition using Arktype.\n *\n * @param value - The model value to validate\n * @returns The validated model if structure is valid\n * @throws Error if the model structure is invalid\n */\nexport function validateModel(value: unknown): ModelDefinition {\n const result = ModelSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Model validation failed: ${messages}`);\n }\n return result as ModelDefinition;\n}\n\n/**\n * Validates the structural shape of a SqlContract using Arktype.\n *\n * **Responsibility: Validation Only**\n * This function validates that the contract has the correct structure and types.\n * It does NOT normalize the contract - normalization must happen in the contract builder.\n *\n * The contract passed to this function must already be normalized (all required fields present).\n * If normalization is needed, it should be done by the contract builder before calling this function.\n *\n * This ensures all required fields are present and have the correct types.\n *\n * @param value - The contract value to validate (typically from a JSON import)\n * @returns The validated contract if structure is valid\n * @throws Error if the contract structure is invalid\n */\nexport function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {\n if (typeof value !== 'object' || value === null) {\n throw new Error('Contract structural validation failed: value must be an object');\n }\n\n // Check targetFamily first to provide a clear error message for unsupported target families\n const rawValue = value as { targetFamily?: string };\n if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {\n throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);\n }\n\n const contractResult = SqlContractSchema(value);\n\n if (contractResult instanceof type.errors) {\n const messages = contractResult.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Contract structural validation failed: ${messages}`);\n }\n\n // After validation, contractResult matches the schema and preserves the input structure\n // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences\n // between Arktype's inferred type and the generic T, but runtime-wise they're compatible\n return contractResult as T;\n}\n\n/**\n * Validates semantic constraints on SqlStorage that cannot be expressed in Arktype schemas.\n *\n * Returns an array of human-readable error strings. Empty array = valid.\n *\n * Currently checks:\n * - `setNull` referential action on a non-nullable FK column (would fail at runtime)\n * - `setDefault` referential action on a non-nullable FK column without a DEFAULT (would fail at runtime)\n */\nexport function validateStorageSemantics(storage: SqlStorage): string[] {\n const errors: string[] = [];\n\n for (const [tableName, table] of Object.entries(storage.tables)) {\n for (const fk of table.foreignKeys) {\n for (const colName of fk.columns) {\n const column = table.columns[colName];\n if (!column) continue;\n\n if (fk.onDelete === 'setNull' && !column.nullable) {\n errors.push(\n `Table \"${tableName}\": onDelete setNull on foreign key column \"${colName}\" which is NOT NULL`,\n );\n }\n if (fk.onUpdate === 'setNull' && !column.nullable) {\n errors.push(\n `Table \"${tableName}\": onUpdate setNull on foreign key column \"${colName}\" which is NOT NULL`,\n );\n }\n if (fk.onDelete === 'setDefault' && !column.nullable && column.default === undefined) {\n errors.push(\n `Table \"${tableName}\": onDelete setDefault on foreign key column \"${colName}\" which is NOT NULL and has no DEFAULT`,\n );\n }\n if (fk.onUpdate === 'setDefault' && !column.nullable && column.default === undefined) {\n errors.push(\n `Table \"${tableName}\": onUpdate setDefault on foreign key column \"${colName}\" which is NOT NULL and has no DEFAULT`,\n );\n }\n }\n }\n }\n\n return errors;\n}\n"],"mappings":";;;AAkBA,MAAM,oBAAoB,KAAK,YAAY;AAC3C,MAAM,qBAAqB,KAAK,aAAa;AAC7C,MAAM,sBAAsB,KAAK,cAAc;AAC/C,MAAM,oBAAoB,KAAK,SAAS,CAAC,QAAQ,OAAO,QAAQ;AAC9D,QAAO,8BAA8B,KAAK,MAAM,GAAG,OAAO,IAAI,OAAO,sBAAsB;EAC3F;AAEF,MAAa,6BAA6B,KAAK,SAA+B,CAAC,KAAK;CAClF,MAAM;CACN,OAAO;CACR,CAAC;AAEF,MAAa,8BAA8B,KAAK,SAAgC,CAAC,KAAK;CACpF,MAAM;CACN,YAAY;CACb,CAAC;AAEF,MAAa,sBAAsB,2BAA2B,GAAG,4BAA4B;AAE7F,MAAM,sCAAsC,KAAK;CAC/C,KAAK;CACL,MAAM;CACN,IAAI;CACJ,WAAW;CACZ,CAAC;AAEF,MAAM,iC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