cogsbox-shape 0.5.205 → 0.5.206
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 +43 -41
- package/cogsbox-shape-state/dist/example/type-hover-example.js +2 -2
- package/dist/schema.d.ts +51 -50
- package/dist/schema.js +90 -83
- package/dist/vitest/fullSchema.test.js +249 -127
- package/dist/vitest/refineRuntime.test.js +57 -18
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -49,7 +49,7 @@ function createSqlBuilder(dialect, sqlConfig) {
|
|
|
49
49
|
validationZod: sqlZodType,
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
|
-
function
|
|
52
|
+
function isClientOptions(value) {
|
|
53
53
|
return (value !== undefined &&
|
|
54
54
|
typeof value === "object" &&
|
|
55
55
|
value !== null &&
|
|
@@ -59,9 +59,9 @@ function isClientInputOptions(value) {
|
|
|
59
59
|
("value" in value || "schema" in value || "clientPk" in value));
|
|
60
60
|
}
|
|
61
61
|
export const s = {
|
|
62
|
-
|
|
62
|
+
client: (...args) => {
|
|
63
63
|
const first = args[0];
|
|
64
|
-
if (
|
|
64
|
+
if (isClientOptions(first)) {
|
|
65
65
|
return createBuilder({
|
|
66
66
|
stage: "sql",
|
|
67
67
|
sqlConfig: null,
|
|
@@ -69,7 +69,7 @@ export const s = {
|
|
|
69
69
|
initialValue: undefined,
|
|
70
70
|
clientZod: z.undefined(),
|
|
71
71
|
validationZod: z.undefined(),
|
|
72
|
-
}).
|
|
72
|
+
}).client(first);
|
|
73
73
|
}
|
|
74
74
|
const value = first;
|
|
75
75
|
const sample = isFunction(value) ? value({ uuid }) : value;
|
|
@@ -93,7 +93,7 @@ export const s = {
|
|
|
93
93
|
inferredZodType = z.any();
|
|
94
94
|
}
|
|
95
95
|
return createBuilder({
|
|
96
|
-
stage: "
|
|
96
|
+
stage: "client",
|
|
97
97
|
sqlConfig: null,
|
|
98
98
|
sqlZod: z.undefined(),
|
|
99
99
|
initialValue: value,
|
|
@@ -133,10 +133,10 @@ function createBuilder(config) {
|
|
|
133
133
|
config: {
|
|
134
134
|
sql: config.sqlConfig,
|
|
135
135
|
zodSqlSchema: config.sqlZod,
|
|
136
|
-
initialValue: config.initialValue
|
|
136
|
+
initialValue: config.initialValue ??
|
|
137
137
|
inferDefaultFromZod(config.clientZod, config.sqlConfig),
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
zodClientSchema: config.clientInputZod || config.clientZod,
|
|
139
|
+
zodClientCheckedSchema: config.clientZod,
|
|
140
140
|
zodValidationSchema: config.validationZod,
|
|
141
141
|
clientTransform: config.clientTransform,
|
|
142
142
|
validationTransform: config.validationTransform,
|
|
@@ -150,15 +150,15 @@ function createBuilder(config) {
|
|
|
150
150
|
},
|
|
151
151
|
});
|
|
152
152
|
},
|
|
153
|
-
|
|
154
|
-
if (completedStages.has("
|
|
155
|
-
throw new Error("
|
|
153
|
+
client: (...args) => {
|
|
154
|
+
if (completedStages.has("client")) {
|
|
155
|
+
throw new Error("client() can only be called once in the chain");
|
|
156
156
|
}
|
|
157
157
|
if (completedStages.has("server")) {
|
|
158
|
-
throw new Error("
|
|
158
|
+
throw new Error("client() must be called before server()");
|
|
159
159
|
}
|
|
160
160
|
const newCompletedStages = new Set(completedStages);
|
|
161
|
-
newCompletedStages.add("
|
|
161
|
+
newCompletedStages.add("client");
|
|
162
162
|
let optionsOrSchema = args[0];
|
|
163
163
|
if (config.stage === "relation") {
|
|
164
164
|
const assert = typeof optionsOrSchema === "function" ||
|
|
@@ -167,7 +167,7 @@ function createBuilder(config) {
|
|
|
167
167
|
: optionsOrSchema?.schema;
|
|
168
168
|
return createBuilder({
|
|
169
169
|
...config,
|
|
170
|
-
stage: "
|
|
170
|
+
stage: "client",
|
|
171
171
|
completedStages: newCompletedStages,
|
|
172
172
|
clientZod: assert,
|
|
173
173
|
clientInputZod: assert,
|
|
@@ -215,14 +215,6 @@ function createBuilder(config) {
|
|
|
215
215
|
if (value !== undefined) {
|
|
216
216
|
actualValue = value;
|
|
217
217
|
}
|
|
218
|
-
else if (schemaOrModifier &&
|
|
219
|
-
typeof schemaOrModifier === "object" &&
|
|
220
|
-
"_def" in schemaOrModifier) {
|
|
221
|
-
if (config.sqlZod instanceof z.ZodUndefined ||
|
|
222
|
-
actualValue === undefined) {
|
|
223
|
-
actualValue = inferDefaultFromZod(schemaOrModifier, config.sqlConfig);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
218
|
let baseSchema;
|
|
227
219
|
if (schemaOrModifier &&
|
|
228
220
|
typeof schemaOrModifier === "object" &&
|
|
@@ -260,6 +252,12 @@ function createBuilder(config) {
|
|
|
260
252
|
finalSchema = baseSchema;
|
|
261
253
|
}
|
|
262
254
|
}
|
|
255
|
+
if (value === undefined) {
|
|
256
|
+
const inferredClientDefault = inferDefaultFromZod(finalSchema);
|
|
257
|
+
if (inferredClientDefault !== undefined) {
|
|
258
|
+
actualValue = inferredClientDefault;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
263
261
|
const newConfig = { ...config.sqlConfig };
|
|
264
262
|
if (clientPk !== undefined) {
|
|
265
263
|
newConfig.isClientPk = clientPk;
|
|
@@ -286,7 +284,7 @@ function createBuilder(config) {
|
|
|
286
284
|
}
|
|
287
285
|
return createBuilder({
|
|
288
286
|
...config,
|
|
289
|
-
stage: "
|
|
287
|
+
stage: "client",
|
|
290
288
|
sqlConfig: newConfig,
|
|
291
289
|
initialValue: actualValue,
|
|
292
290
|
clientZod: clientAndServerSchema,
|
|
@@ -295,15 +293,15 @@ function createBuilder(config) {
|
|
|
295
293
|
completedStages: newCompletedStages,
|
|
296
294
|
});
|
|
297
295
|
},
|
|
298
|
-
|
|
296
|
+
clientCheck: (assert) => {
|
|
299
297
|
if (completedStages.has("server")) {
|
|
300
298
|
throw new Error("client() must be called before server()");
|
|
301
299
|
}
|
|
302
300
|
const clientSchema = isFunction(assert)
|
|
303
301
|
? assert({
|
|
304
302
|
sql: config.sqlZod,
|
|
305
|
-
|
|
306
|
-
|
|
303
|
+
client: config.clientInputZod || config.clientZod,
|
|
304
|
+
clientCheck: config.clientZod,
|
|
307
305
|
})
|
|
308
306
|
: assert;
|
|
309
307
|
const newCompletedStages = new Set(completedStages);
|
|
@@ -327,8 +325,8 @@ function createBuilder(config) {
|
|
|
327
325
|
const serverSchema = isFunction(assert)
|
|
328
326
|
? assert({
|
|
329
327
|
sql: config.sqlZod,
|
|
330
|
-
|
|
331
|
-
|
|
328
|
+
client: config.clientInputZod || config.clientZod,
|
|
329
|
+
clientCheck: config.clientZod,
|
|
332
330
|
})
|
|
333
331
|
: assert;
|
|
334
332
|
const newCompletedStages = new Set(completedStages);
|
|
@@ -342,8 +340,8 @@ function createBuilder(config) {
|
|
|
342
340
|
},
|
|
343
341
|
transform: (transforms) => {
|
|
344
342
|
if (!completedStages.has("server") &&
|
|
345
|
-
!completedStages.has("
|
|
346
|
-
throw new Error("transform() requires at least
|
|
343
|
+
!completedStages.has("client")) {
|
|
344
|
+
throw new Error("transform() requires at least client() or server() to be called first");
|
|
347
345
|
}
|
|
348
346
|
return {
|
|
349
347
|
config: {
|
|
@@ -420,6 +418,43 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
420
418
|
if ("default" in sqlConfig && sqlConfig.default !== undefined) {
|
|
421
419
|
return sqlConfig.default;
|
|
422
420
|
}
|
|
421
|
+
const sqlTypeConfig = sqlConfig;
|
|
422
|
+
if (sqlTypeConfig.nullable) {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (zodType instanceof z.ZodDefault) {
|
|
427
|
+
const def = zodType._def;
|
|
428
|
+
const val = def.defaultValue;
|
|
429
|
+
if (val !== undefined) {
|
|
430
|
+
return typeof val === "function" ? val() : val;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (zodType instanceof z.ZodNullable) {
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
if (zodType instanceof z.ZodOptional) {
|
|
437
|
+
return undefined;
|
|
438
|
+
}
|
|
439
|
+
if (zodType instanceof z.ZodString) {
|
|
440
|
+
return "";
|
|
441
|
+
}
|
|
442
|
+
if (zodType instanceof z.ZodNumber) {
|
|
443
|
+
return 0;
|
|
444
|
+
}
|
|
445
|
+
if (zodType instanceof z.ZodBoolean) {
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
if (zodType instanceof z.ZodDate) {
|
|
449
|
+
return new Date();
|
|
450
|
+
}
|
|
451
|
+
if (zodType instanceof z.ZodEnum) {
|
|
452
|
+
return zodType.options[0];
|
|
453
|
+
}
|
|
454
|
+
if (zodType instanceof z.ZodLiteral) {
|
|
455
|
+
return zodType.value;
|
|
456
|
+
}
|
|
457
|
+
if (sqlConfig && typeof sqlConfig === "object" && "type" in sqlConfig) {
|
|
423
458
|
const sqlTypeConfig = sqlConfig;
|
|
424
459
|
if (sqlTypeConfig.type && !sqlTypeConfig.nullable) {
|
|
425
460
|
switch (sqlTypeConfig.type) {
|
|
@@ -444,16 +479,6 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
444
479
|
return null;
|
|
445
480
|
}
|
|
446
481
|
}
|
|
447
|
-
if ("_def" in zodType && "defaultValue" in zodType._def) {
|
|
448
|
-
const def = zodType._def;
|
|
449
|
-
const val = def.defaultValue;
|
|
450
|
-
if (val !== undefined) {
|
|
451
|
-
return typeof val === "function" ? val() : val;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
if (zodType instanceof z.ZodString) {
|
|
455
|
-
return "";
|
|
456
|
-
}
|
|
457
482
|
return undefined;
|
|
458
483
|
}
|
|
459
484
|
function isReference(value) {
|
|
@@ -461,8 +486,8 @@ function isReference(value) {
|
|
|
461
486
|
}
|
|
462
487
|
export function createSchema(schema, relations) {
|
|
463
488
|
const sqlFields = {};
|
|
464
|
-
const clientInputFields = {};
|
|
465
489
|
const clientFields = {};
|
|
490
|
+
const clientCheckedFields = {};
|
|
466
491
|
const serverFields = {};
|
|
467
492
|
const defaultValues = {};
|
|
468
493
|
const defaultGenerators = {};
|
|
@@ -497,20 +522,14 @@ export function createSchema(schema, relations) {
|
|
|
497
522
|
else {
|
|
498
523
|
clientToDbKeys[key] = dbFieldName;
|
|
499
524
|
dbToClientKeys[dbFieldName] = key;
|
|
500
|
-
clientInputFields[key] = config.zodClientInputSchema;
|
|
501
525
|
clientFields[key] = config.zodClientSchema;
|
|
526
|
+
clientCheckedFields[key] = config.zodClientCheckedSchema;
|
|
502
527
|
serverFields[key] = config.zodValidationSchema;
|
|
503
528
|
const initialValueOrFn = config.initialValue;
|
|
504
529
|
defaultGenerators[key] = initialValueOrFn;
|
|
505
|
-
|
|
530
|
+
defaultValues[key] = isFunction(initialValueOrFn)
|
|
506
531
|
? initialValueOrFn({ uuid })
|
|
507
532
|
: initialValueOrFn;
|
|
508
|
-
if (config.transforms?.toClient && rawDefault !== undefined) {
|
|
509
|
-
defaultValues[key] = config.transforms.toClient(rawDefault);
|
|
510
|
-
}
|
|
511
|
-
else {
|
|
512
|
-
defaultValues[key] = rawDefault;
|
|
513
|
-
}
|
|
514
533
|
if (config.transforms) {
|
|
515
534
|
fieldTransforms[key] = config.transforms;
|
|
516
535
|
}
|
|
@@ -539,43 +558,31 @@ export function createSchema(schema, relations) {
|
|
|
539
558
|
else {
|
|
540
559
|
clientToDbKeys[key] = dbFieldName;
|
|
541
560
|
dbToClientKeys[dbFieldName] = key;
|
|
542
|
-
clientInputFields[key] = config.zodClientInputSchema;
|
|
543
561
|
clientFields[key] = config.zodClientSchema;
|
|
562
|
+
clientCheckedFields[key] = config.zodClientCheckedSchema;
|
|
544
563
|
serverFields[key] = config.zodValidationSchema;
|
|
545
564
|
if (config.transforms) {
|
|
546
565
|
fieldTransforms[key] = config.transforms;
|
|
547
566
|
}
|
|
548
567
|
const initialValueOrFn = config.initialValue;
|
|
549
568
|
defaultGenerators[key] = initialValueOrFn;
|
|
550
|
-
|
|
569
|
+
defaultValues[key] = isFunction(initialValueOrFn)
|
|
551
570
|
? initialValueOrFn({ uuid })
|
|
552
571
|
: initialValueOrFn;
|
|
553
|
-
if (config.transforms?.toClient && rawDefault !== undefined) {
|
|
554
|
-
defaultValues[key] = config.transforms.toClient(rawDefault);
|
|
555
|
-
}
|
|
556
|
-
else {
|
|
557
|
-
defaultValues[key] = rawDefault;
|
|
558
|
-
}
|
|
559
572
|
}
|
|
560
573
|
}
|
|
561
574
|
else {
|
|
562
|
-
clientInputFields[key] = config.zodClientInputSchema;
|
|
563
575
|
clientFields[key] = config.zodClientSchema;
|
|
576
|
+
clientCheckedFields[key] = config.zodClientCheckedSchema;
|
|
564
577
|
serverFields[key] = config.zodValidationSchema;
|
|
565
578
|
if (config.transforms) {
|
|
566
579
|
fieldTransforms[key] = config.transforms;
|
|
567
580
|
}
|
|
568
581
|
const initialValueOrFn = config.initialValue;
|
|
569
582
|
defaultGenerators[key] = initialValueOrFn;
|
|
570
|
-
|
|
583
|
+
defaultValues[key] = isFunction(initialValueOrFn)
|
|
571
584
|
? initialValueOrFn({ uuid })
|
|
572
585
|
: initialValueOrFn;
|
|
573
|
-
if (config.transforms?.toClient && rawDefault !== undefined) {
|
|
574
|
-
defaultValues[key] = config.transforms.toClient(rawDefault);
|
|
575
|
-
}
|
|
576
|
-
else {
|
|
577
|
-
defaultValues[key] = rawDefault;
|
|
578
|
-
}
|
|
579
586
|
}
|
|
580
587
|
}
|
|
581
588
|
}
|
|
@@ -622,14 +629,10 @@ export function createSchema(schema, relations) {
|
|
|
622
629
|
const generateDefaults = () => {
|
|
623
630
|
const freshDefaults = {};
|
|
624
631
|
for (const key in defaultGenerators) {
|
|
625
|
-
// ... same logic for mapping standard defaults ...
|
|
626
632
|
const generatorOrValue = defaultGenerators[key];
|
|
627
|
-
|
|
633
|
+
freshDefaults[key] = isFunction(generatorOrValue)
|
|
628
634
|
? generatorOrValue({ uuid })
|
|
629
635
|
: generatorOrValue;
|
|
630
|
-
freshDefaults[key] = fieldTransforms[key]?.toClient
|
|
631
|
-
? fieldTransforms[key].toClient(rawValue)
|
|
632
|
-
: rawValue;
|
|
633
636
|
}
|
|
634
637
|
// Only apply client derivations
|
|
635
638
|
if (derives?.forClient) {
|
|
@@ -686,8 +689,8 @@ export function createSchema(schema, relations) {
|
|
|
686
689
|
return dbObject;
|
|
687
690
|
};
|
|
688
691
|
const finalSqlSchema = z.object(sqlFields);
|
|
689
|
-
const finalClientInputSchema = z.object(clientInputFields);
|
|
690
692
|
const finalClientSchema = z.object(clientFields);
|
|
693
|
+
const finalClientCheckedSchema = z.object(clientCheckedFields);
|
|
691
694
|
const finalValidationSchema = z.object(serverFields);
|
|
692
695
|
const deriveDependencies = {};
|
|
693
696
|
const trackDeriveDependencies = (deriveGroup) => {
|
|
@@ -714,8 +717,8 @@ export function createSchema(schema, relations) {
|
|
|
714
717
|
trackDeriveDependencies(derives?.forClient);
|
|
715
718
|
trackDeriveDependencies(derives?.forDb);
|
|
716
719
|
let refinedSqlSchema = finalSqlSchema;
|
|
717
|
-
let refinedClientInputSchema = finalClientInputSchema;
|
|
718
720
|
let refinedClientSchema = finalClientSchema;
|
|
721
|
+
let refinedClientCheckedSchema = finalClientCheckedSchema;
|
|
719
722
|
let refinedValidationSchema = finalValidationSchema;
|
|
720
723
|
const fieldToGroup = {};
|
|
721
724
|
if (refineGroups) {
|
|
@@ -723,7 +726,7 @@ export function createSchema(schema, relations) {
|
|
|
723
726
|
const entry = refineGroups[i];
|
|
724
727
|
let { layers, deps, check } = entry;
|
|
725
728
|
const applyTo = layers.includes("all")
|
|
726
|
-
? ["sql", "
|
|
729
|
+
? ["sql", "client", "clientCheck", "server"]
|
|
727
730
|
: layers;
|
|
728
731
|
// Track deps from proxy if not provided explicitly
|
|
729
732
|
if (!deps) {
|
|
@@ -765,12 +768,12 @@ export function createSchema(schema, relations) {
|
|
|
765
768
|
case "sql":
|
|
766
769
|
refinedSqlSchema = refinedSqlSchema.superRefine(refineFn);
|
|
767
770
|
break;
|
|
768
|
-
case "clientInput":
|
|
769
|
-
refinedClientInputSchema = refinedClientInputSchema.superRefine(refineFn);
|
|
770
|
-
break;
|
|
771
771
|
case "client":
|
|
772
772
|
refinedClientSchema = refinedClientSchema.superRefine(refineFn);
|
|
773
773
|
break;
|
|
774
|
+
case "clientCheck":
|
|
775
|
+
refinedClientCheckedSchema = refinedClientCheckedSchema.superRefine(refineFn);
|
|
776
|
+
break;
|
|
774
777
|
case "server":
|
|
775
778
|
refinedValidationSchema = refinedValidationSchema.superRefine(refineFn);
|
|
776
779
|
break;
|
|
@@ -785,8 +788,8 @@ export function createSchema(schema, relations) {
|
|
|
785
788
|
refineInfo: { groups: refineGroups ?? [], fieldToGroup },
|
|
786
789
|
isClientRecord,
|
|
787
790
|
sqlSchema: refinedSqlSchema,
|
|
788
|
-
clientInputSchema: refinedClientInputSchema,
|
|
789
791
|
clientSchema: refinedClientSchema,
|
|
792
|
+
clientCheckedSchema: refinedClientCheckedSchema,
|
|
790
793
|
serverSchema: refinedValidationSchema,
|
|
791
794
|
defaultValues: defaultValues,
|
|
792
795
|
stateType: {},
|
|
@@ -825,11 +828,13 @@ function createViewObject(initialRegistryKey, selection, registry, tableNameToRe
|
|
|
825
828
|
allTablesSupportsReconciliation = false;
|
|
826
829
|
}
|
|
827
830
|
}
|
|
828
|
-
const baseSchema = schemaType === "
|
|
829
|
-
? registryEntry.zodSchemas.
|
|
830
|
-
: schemaType === "
|
|
831
|
-
? registryEntry.zodSchemas.
|
|
832
|
-
:
|
|
831
|
+
const baseSchema = schemaType === "clientChecked"
|
|
832
|
+
? registryEntry.zodSchemas.clientCheckedSchema
|
|
833
|
+
: schemaType === "server"
|
|
834
|
+
? registryEntry.zodSchemas.serverSchema
|
|
835
|
+
: schemaType === "sql"
|
|
836
|
+
? registryEntry.zodSchemas.sqlSchema
|
|
837
|
+
: registryEntry.zodSchemas.clientSchema;
|
|
833
838
|
const primitiveShape = baseSchema.shape;
|
|
834
839
|
if (subSelection === true) {
|
|
835
840
|
return baseSchema;
|
|
@@ -862,6 +867,7 @@ function createViewObject(initialRegistryKey, selection, registry, tableNameToRe
|
|
|
862
867
|
return {
|
|
863
868
|
sql: buildView(initialRegistryKey, selection, "sql"),
|
|
864
869
|
client: buildView(initialRegistryKey, selection, "client"),
|
|
870
|
+
clientChecked: buildView(initialRegistryKey, selection, "clientChecked"),
|
|
865
871
|
server: buildView(initialRegistryKey, selection, "server"),
|
|
866
872
|
supportsReconciliation: allTablesSupportsReconciliation,
|
|
867
873
|
};
|
|
@@ -1018,8 +1024,8 @@ export function createSchemaBox(schemas, resolutions) {
|
|
|
1018
1024
|
schemaKey: tableName,
|
|
1019
1025
|
schemas: {
|
|
1020
1026
|
sql: entry.zodSchemas.sqlSchema,
|
|
1021
|
-
clientInput: entry.zodSchemas.clientInputSchema,
|
|
1022
1027
|
client: entry.zodSchemas.clientSchema,
|
|
1028
|
+
clientChecked: entry.zodSchemas.clientCheckedSchema,
|
|
1023
1029
|
server: entry.zodSchemas.serverSchema,
|
|
1024
1030
|
},
|
|
1025
1031
|
transforms: {
|
|
@@ -1163,6 +1169,7 @@ export function createSchemaBox(schemas, resolutions) {
|
|
|
1163
1169
|
schemas: {
|
|
1164
1170
|
sql: view.sql,
|
|
1165
1171
|
client: view.client,
|
|
1172
|
+
clientChecked: view.clientChecked,
|
|
1166
1173
|
server: view.server,
|
|
1167
1174
|
},
|
|
1168
1175
|
transforms: {
|