@prisma-next/sql-contract 0.3.0-dev.123 → 0.3.0-dev.125
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/dist/factories.d.mts +1 -1
- package/dist/{types-mnqqP_Au.d.mts → types-C7hjlEOo.d.mts} +17 -5
- package/dist/types-C7hjlEOo.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 +246 -53
- package/dist/validate.mjs.map +1 -1
- package/dist/{validators-DuHeCiKZ.mjs → validators-B1ZW3frd.mjs} +18 -6
- package/dist/validators-B1ZW3frd.mjs.map +1 -0
- package/dist/validators.d.mts +1 -1
- package/dist/validators.d.mts.map +1 -1
- package/dist/validators.mjs +1 -1
- package/package.json +4 -4
- package/src/construct.ts +4 -1
- package/src/exports/types.ts +3 -0
- package/src/types.ts +18 -2
- package/src/validate.ts +345 -57
- package/src/validators.ts +16 -6
- package/dist/types-mnqqP_Au.d.mts.map +0 -1
- package/dist/validators-DuHeCiKZ.mjs.map +0 -1
|
@@ -97,12 +97,23 @@ const StorageSchema = type({
|
|
|
97
97
|
tables: type({ "[string]": StorageTableSchema }),
|
|
98
98
|
"types?": type({ "[string]": StorageTypeInstanceSchema })
|
|
99
99
|
});
|
|
100
|
-
const ModelFieldSchema = type
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
const ModelFieldSchema = type({
|
|
101
|
+
"column?": "string",
|
|
102
|
+
"nullable?": "boolean",
|
|
103
|
+
"codecId?": "string"
|
|
104
|
+
});
|
|
105
|
+
const ModelStorageFieldSchema = type({ column: "string" });
|
|
106
|
+
const ModelSchema = type({
|
|
107
|
+
storage: type({
|
|
108
|
+
table: "string",
|
|
109
|
+
"fields?": type({ "[string]": ModelStorageFieldSchema })
|
|
110
|
+
}),
|
|
104
111
|
fields: type({ "[string]": ModelFieldSchema }),
|
|
105
|
-
relations: type({ "[string]": "unknown" })
|
|
112
|
+
relations: type({ "[string]": "unknown" }),
|
|
113
|
+
"discriminator?": "unknown",
|
|
114
|
+
"variants?": "unknown",
|
|
115
|
+
"base?": "string",
|
|
116
|
+
"owner?": "string"
|
|
106
117
|
});
|
|
107
118
|
const MappingsSchema = type({
|
|
108
119
|
"+": "reject",
|
|
@@ -129,6 +140,7 @@ const SqlContractSchema = type({
|
|
|
129
140
|
"meta?": ContractMetaSchema,
|
|
130
141
|
"sources?": "Record<string, unknown>",
|
|
131
142
|
"relations?": type({ "[string]": "unknown" }),
|
|
143
|
+
"roots?": "Record<string, string>",
|
|
132
144
|
"mappings?": MappingsSchema,
|
|
133
145
|
models: type({ "[string]": ModelSchema }),
|
|
134
146
|
storage: StorageSchema,
|
|
@@ -215,4 +227,4 @@ function validateStorageSemantics(storage) {
|
|
|
215
227
|
|
|
216
228
|
//#endregion
|
|
217
229
|
export { ForeignKeySchema as a, validateModel as c, validateStorageSemantics as d, ForeignKeyReferencesSchema as i, validateSqlContract as l, ColumnDefaultLiteralSchema as n, IndexSchema as o, ColumnDefaultSchema as r, ReferentialActionSchema as s, ColumnDefaultFunctionSchema as t, validateStorage as u };
|
|
218
|
-
//# sourceMappingURL=validators-
|
|
230
|
+
//# sourceMappingURL=validators-B1ZW3frd.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators-B1ZW3frd.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});\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,iCAAiC,KAAK;CAC1C,KAAK;CACL,KAAK;EACH,KAAK;EACL,OAAO;EACP,QAAQ;EACT;CACD,aAAa;CACb,aAAa;CACd,CAAC;AAEF,MAAM,kBAAkB,KAAK;CAC3B,KAAK;CACL,WAAW;EACT,KAAK;EACL,UAAU,+BAA+B,OAAO,CAAC,UAAU;EAC5D;CACF,CAAC;AAEF,MAAM,sBAAsB,KAAK;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AAC,OAAO,YAAY,OAAO,YAAY,OACzE,QAAO,KACL,UAAU,UAAU,gDAAgD,QAAQ,wCAC7E;;AAMT,QAAO"}
|
package/dist/validators.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as SqlStorage, d as ForeignKeyReferences, l as ForeignKey, p as ModelDefinition, w as SqlContract, x as ReferentialAction } from "./types-C7hjlEOo.mjs";
|
|
2
2
|
import * as arktype_internal_variants_object_ts0 from "arktype/internal/variants/object.ts";
|
|
3
3
|
import * as arktype_internal_variants_string_ts0 from "arktype/internal/variants/string.ts";
|
|
4
4
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.mts","names":[],"sources":["../src/validators.ts"],"sourcesContent":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"validators.d.mts","names":[],"sources":["../src/validators.ts"],"sourcesContent":[],"mappings":";;;;;KAaK,oBAAA;;8CAEyC;;AAJ7B,KAMZ,qBAAA,GAJoB;EAIpB,SAAA,IAAA,EAAA,UAAqB;EAQb,SAAA,UAAA,EAAA,MAAA;AAKb,CAAA;AAKa,cAVA,0BAUgF,EAVtD,oCAAA,CAAA,UAUsD,CAVtD,oBAUsD,EAAA,CAAA,CAAA,CAAA;AAA7D,cALnB,2BAKmB,EALQ,oCAAA,CAAA,UAKR,CALQ,qBAKR,EAAA,CAAA,CAAA,CAAA;AAAA,cAAnB,mBAAmB,EAAA,oCAAA,CAAA,UAAA,CAAA,oBAAA,GAAA,qBAAA,EAAA,CAAA,CAAA,CAAA;AAAA,cA2DnB,WA3DmB,EAgE9B,oCAAA,CALsB,UA3DQ,CAAA;EAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EA2DnB,IAAA,CAAA,EAAA,MAAA;EAOA,KAAA,CAAA,EAAA,MAAA;EAKA,MAAA,CAAA,EAPX,MAOW,CAAA,MAAA,EAAA,OAE4D,CAAA;AAEzE,CAAA,EAAA,CAAa,CAAA,CAAA;AAoGG,cA7GH,0BA6G8C,EA7GpB,oCAAA,CAAA,UA6GoB,CA7GpB,oBA6GoB,EAAA,CAAA,CAAA,CAAA;AAgB3C,cAxHH,uBAwHkC,EAxHX,oCAAA,CAAA,UAwH0B,CAxH1B,iBAwH0B,EAAA,CAAA,CAAA,CAAA;AAyB9C,cA7IH,gBA6IsB,EA7IN,oCAAA,CAAA,UA6IM,CA7IN,UA6IM,EAAA,CAAA,CAAA,CAAA;;;;;AAiCnC;;;iBA1EgB,eAAA,kBAAiC;;;;;;;;iBAgBjC,aAAA,kBAA+B;;;;;;;;;;;;;;;;;iBAyB/B,8BAA8B,YAAY,8BAA8B;;;;;;;;;;iBAiCxE,wBAAA,UAAkC"}
|
package/dist/validators.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as ForeignKeySchema, c as validateModel, d as validateStorageSemantics, i as ForeignKeyReferencesSchema, l as validateSqlContract, n as ColumnDefaultLiteralSchema, o as IndexSchema, r as ColumnDefaultSchema, s as ReferentialActionSchema, t as ColumnDefaultFunctionSchema, u as validateStorage } from "./validators-
|
|
1
|
+
import { a as ForeignKeySchema, c as validateModel, d as validateStorageSemantics, i as ForeignKeyReferencesSchema, l as validateSqlContract, n as ColumnDefaultLiteralSchema, o as IndexSchema, r as ColumnDefaultSchema, s as ReferentialActionSchema, t as ColumnDefaultFunctionSchema, u as validateStorage } from "./validators-B1ZW3frd.mjs";
|
|
2
2
|
|
|
3
3
|
export { ColumnDefaultFunctionSchema, ColumnDefaultLiteralSchema, ColumnDefaultSchema, ForeignKeyReferencesSchema, ForeignKeySchema, IndexSchema, ReferentialActionSchema, validateModel, validateSqlContract, validateStorage, validateStorageSemantics };
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.125",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "SQL contract types, validators, and IR factories for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"arktype": "^2.1.25",
|
|
9
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
9
|
+
"@prisma-next/contract": "0.3.0-dev.125"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"tsdown": "0.18.4",
|
|
13
13
|
"typescript": "5.9.3",
|
|
14
14
|
"vitest": "4.0.17",
|
|
15
|
-
"@prisma-next/test-utils": "0.0.1",
|
|
16
15
|
"@prisma-next/tsconfig": "0.0.0",
|
|
17
|
-
"@prisma-next/tsdown": "0.0.0"
|
|
16
|
+
"@prisma-next/tsdown": "0.0.0",
|
|
17
|
+
"@prisma-next/test-utils": "0.0.1"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
package/src/construct.ts
CHANGED
|
@@ -169,9 +169,12 @@ export function constructContract<TContract extends SqlContract<SqlStorage>>(
|
|
|
169
169
|
const defaultMappings = computeDefaultMappings(input.models as Record<string, ModelDefinition>);
|
|
170
170
|
const mappings = mergeMappings(defaultMappings, existingMappings);
|
|
171
171
|
|
|
172
|
+
const stripped = stripGenerated(input);
|
|
173
|
+
|
|
172
174
|
const contractWithMappings = {
|
|
173
|
-
...
|
|
175
|
+
...stripped,
|
|
174
176
|
mappings,
|
|
177
|
+
roots: stripped.roots,
|
|
175
178
|
};
|
|
176
179
|
|
|
177
180
|
return contractWithMappings as TContract;
|
package/src/exports/types.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ColumnDefault,
|
|
3
3
|
ContractBase,
|
|
4
|
+
DomainRelationOn,
|
|
4
5
|
ExecutionHashBase,
|
|
5
6
|
ExecutionSection,
|
|
6
7
|
ProfileHashBase,
|
|
@@ -131,6 +132,22 @@ export type ModelDefinition = {
|
|
|
131
132
|
readonly storage: ModelStorage;
|
|
132
133
|
readonly fields: Record<string, ModelField>;
|
|
133
134
|
readonly relations: Record<string, unknown>;
|
|
135
|
+
readonly owner?: string;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export type SqlModelFieldStorage = {
|
|
139
|
+
readonly column: string;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export type SqlModelStorage = {
|
|
143
|
+
readonly table: string;
|
|
144
|
+
readonly fields: Record<string, SqlModelFieldStorage>;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export type SqlRelation = {
|
|
148
|
+
readonly to: string;
|
|
149
|
+
readonly cardinality: '1:1' | '1:N' | 'N:1';
|
|
150
|
+
readonly on: DomainRelationOn;
|
|
134
151
|
};
|
|
135
152
|
|
|
136
153
|
export type SqlMappings = {
|
|
@@ -210,10 +227,9 @@ export type SqlContract<
|
|
|
210
227
|
TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
|
|
211
228
|
TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
|
|
212
229
|
TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
|
|
213
|
-
> = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {
|
|
230
|
+
> = ContractBase<TStorageHash, TExecutionHash, TProfileHash, M> & {
|
|
214
231
|
readonly targetFamily: string;
|
|
215
232
|
readonly storage: S;
|
|
216
|
-
readonly models: M;
|
|
217
233
|
readonly relations: R;
|
|
218
234
|
readonly mappings: Map;
|
|
219
235
|
readonly execution?: ExecutionSection;
|
package/src/validate.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import type { ColumnDefaultLiteralInputValue } from '@prisma-next/contract/types';
|
|
2
2
|
import { isTaggedBigInt, isTaggedRaw } from '@prisma-next/contract/types';
|
|
3
|
+
import type { DomainContractShape, DomainModelShape } from '@prisma-next/contract/validate-domain';
|
|
4
|
+
import { validateContractDomain } from '@prisma-next/contract/validate-domain';
|
|
3
5
|
import { constructContract } from './construct';
|
|
4
6
|
import type { SqlContract, SqlStorage, StorageColumn, StorageTable } from './types';
|
|
5
7
|
import { applyFkDefaults } from './types';
|
|
6
8
|
import { validateSqlContract, validateStorageSemantics } from './validators';
|
|
7
9
|
|
|
10
|
+
function extractDomainShape(contract: SqlContract<SqlStorage>): DomainContractShape {
|
|
11
|
+
return {
|
|
12
|
+
roots: contract.roots,
|
|
13
|
+
models: contract.models as Record<string, DomainModelShape>,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
function validateContractLogic(contract: SqlContract<SqlStorage>): void {
|
|
9
18
|
const tableNames = new Set(Object.keys(contract.storage.tables));
|
|
10
19
|
|
|
@@ -172,81 +181,356 @@ export function decodeContractDefaults<T extends SqlContract<SqlStorage>>(contra
|
|
|
172
181
|
} as T;
|
|
173
182
|
}
|
|
174
183
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
184
|
+
function normalizeStorage(contractObj: Record<string, unknown>): Record<string, unknown> {
|
|
185
|
+
const normalizedStorage = contractObj['storage'];
|
|
186
|
+
if (!normalizedStorage || typeof normalizedStorage !== 'object')
|
|
187
|
+
return normalizedStorage as Record<string, unknown>;
|
|
188
|
+
|
|
189
|
+
const storage = normalizedStorage as Record<string, unknown>;
|
|
190
|
+
const tables = storage['tables'] as Record<string, unknown> | undefined;
|
|
191
|
+
if (!tables) return storage;
|
|
192
|
+
|
|
193
|
+
const normalizedTables: Record<string, unknown> = {};
|
|
194
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
195
|
+
const tableObj = table as Record<string, unknown>;
|
|
196
|
+
const columns = tableObj['columns'] as Record<string, unknown> | undefined;
|
|
197
|
+
|
|
198
|
+
if (columns) {
|
|
199
|
+
const normalizedColumns: Record<string, unknown> = {};
|
|
200
|
+
for (const [columnName, column] of Object.entries(columns)) {
|
|
201
|
+
const columnObj = column as Record<string, unknown>;
|
|
202
|
+
normalizedColumns[columnName] = {
|
|
203
|
+
...columnObj,
|
|
204
|
+
nullable: columnObj['nullable'] ?? false,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const rawForeignKeys = (tableObj['foreignKeys'] ?? []) as Array<Record<string, unknown>>;
|
|
209
|
+
const normalizedForeignKeys = rawForeignKeys.map((fk) => ({
|
|
210
|
+
...fk,
|
|
211
|
+
...applyFkDefaults({
|
|
212
|
+
constraint: typeof fk['constraint'] === 'boolean' ? fk['constraint'] : undefined,
|
|
213
|
+
index: typeof fk['index'] === 'boolean' ? fk['index'] : undefined,
|
|
214
|
+
}),
|
|
215
|
+
}));
|
|
216
|
+
|
|
217
|
+
normalizedTables[tableName] = {
|
|
218
|
+
...tableObj,
|
|
219
|
+
columns: normalizedColumns,
|
|
220
|
+
uniques: tableObj['uniques'] ?? [],
|
|
221
|
+
indexes: tableObj['indexes'] ?? [],
|
|
222
|
+
foreignKeys: normalizedForeignKeys,
|
|
223
|
+
};
|
|
224
|
+
} else {
|
|
225
|
+
normalizedTables[tableName] = tableObj;
|
|
226
|
+
}
|
|
178
227
|
}
|
|
179
228
|
|
|
180
|
-
|
|
229
|
+
return { ...storage, tables: normalizedTables };
|
|
230
|
+
}
|
|
181
231
|
|
|
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
|
-
|
|
232
|
+
type RawModel = Record<string, unknown>;
|
|
233
|
+
type RawField = Record<string, unknown>;
|
|
234
|
+
type RawRelation = Record<string, unknown>;
|
|
235
|
+
type RawStorageObj = { tables: Record<string, Record<string, unknown>> };
|
|
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
|
+
|
|
288
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
289
|
+
const fields = (model['fields'] ?? {}) as Record<string, RawField>;
|
|
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] = {
|
|
326
|
+
...model,
|
|
327
|
+
fields: enrichedFields,
|
|
328
|
+
storage: enrichedStorage,
|
|
329
|
+
relations: model['relations'] ?? {},
|
|
330
|
+
};
|
|
331
|
+
}
|
|
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
|
+
);
|
|
220
359
|
} else {
|
|
221
|
-
|
|
360
|
+
targetColumnToField[toModel] = {};
|
|
222
361
|
}
|
|
223
362
|
}
|
|
363
|
+
const targetColToField = targetColumnToField[toModel] ?? {};
|
|
224
364
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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 },
|
|
228
373
|
};
|
|
229
374
|
}
|
|
375
|
+
|
|
376
|
+
enrichedModels[modelName] = {
|
|
377
|
+
...existingModel,
|
|
378
|
+
relations: modelRelations,
|
|
379
|
+
};
|
|
230
380
|
}
|
|
231
381
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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 },
|
|
241
463
|
};
|
|
242
464
|
}
|
|
243
|
-
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return { enrichedModels, topRelations };
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
471
|
+
if (typeof contract !== 'object' || contract === null) {
|
|
472
|
+
return contract as SqlContract<SqlStorage>;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const contractObj = contract as Record<string, unknown>;
|
|
476
|
+
const normalizedStorage = normalizeStorage(contractObj);
|
|
477
|
+
|
|
478
|
+
const rawModels = contractObj['models'];
|
|
479
|
+
if (!rawModels || typeof rawModels !== 'object' || rawModels === null) {
|
|
480
|
+
return {
|
|
481
|
+
...contractObj,
|
|
482
|
+
roots: contractObj['roots'] ?? {},
|
|
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
|
+
}
|
|
492
|
+
|
|
493
|
+
const modelsObj = rawModels as Record<string, RawModel>;
|
|
494
|
+
const format = detectFormat(modelsObj);
|
|
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;
|
|
244
527
|
}
|
|
245
528
|
|
|
246
529
|
return {
|
|
247
530
|
...contractObj,
|
|
531
|
+
roots,
|
|
248
532
|
models: normalizedModels,
|
|
249
|
-
relations:
|
|
533
|
+
relations: topRelations,
|
|
250
534
|
storage: normalizedStorage,
|
|
251
535
|
extensionPacks: contractObj['extensionPacks'] ?? {},
|
|
252
536
|
capabilities: contractObj['capabilities'] ?? {},
|
|
@@ -259,7 +543,11 @@ export function validateContract<TContract extends SqlContract<SqlStorage>>(
|
|
|
259
543
|
value: unknown,
|
|
260
544
|
): TContract {
|
|
261
545
|
const normalized = normalizeContract(value);
|
|
546
|
+
|
|
262
547
|
const structurallyValid = validateSqlContract<SqlContract<SqlStorage>>(normalized);
|
|
548
|
+
|
|
549
|
+
validateContractDomain(extractDomainShape(structurallyValid));
|
|
550
|
+
|
|
263
551
|
validateContractLogic(structurallyValid);
|
|
264
552
|
|
|
265
553
|
const semanticErrors = validateStorageSemantics(structurallyValid.storage);
|