cogsbox-shape 0.5.118 → 0.5.119
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/schema.js +52 -23
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -126,53 +126,82 @@ function createBuilder(config) {
|
|
|
126
126
|
clientTransform: config.clientTransform, // <-- FIX: Make sure transform is passed through
|
|
127
127
|
validationTransform: config.validationTransform, // <-- FIX: Make sure transform is passed through
|
|
128
128
|
},
|
|
129
|
-
initialState: (value,
|
|
129
|
+
initialState: (value, schemaOrModifier) => {
|
|
130
130
|
if (completedStages.has("new")) {
|
|
131
131
|
throw new Error("initialState() can only be called once in the chain");
|
|
132
132
|
}
|
|
133
133
|
let actualValue;
|
|
134
134
|
let baseSchema;
|
|
135
|
-
|
|
135
|
+
let finalSchema;
|
|
136
|
+
// Check if value is a Zod schema (single argument case)
|
|
136
137
|
if (value && typeof value === "object" && "_def" in value) {
|
|
137
138
|
// It's a Zod schema - infer the default value
|
|
138
139
|
baseSchema = value;
|
|
139
140
|
actualValue = inferDefaultFromZod(baseSchema, config.sqlConfig);
|
|
141
|
+
finalSchema = baseSchema;
|
|
140
142
|
}
|
|
141
143
|
else {
|
|
142
144
|
// Get the actual value
|
|
143
145
|
actualValue = isFunction(value) ? value() : value;
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
baseSchema = z.literal(actualValue);
|
|
146
|
+
// If second parameter is provided and is a Zod schema, use it directly
|
|
147
|
+
if (schemaOrModifier &&
|
|
148
|
+
typeof schemaOrModifier === "object" &&
|
|
149
|
+
"_def" in schemaOrModifier) {
|
|
150
|
+
finalSchema = schemaOrModifier;
|
|
150
151
|
}
|
|
151
|
-
else if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
152
|
+
else if (isFunction(schemaOrModifier)) {
|
|
153
|
+
// It's a schema modifier function
|
|
154
|
+
// Create base Zod schema from the value type
|
|
155
|
+
if (typeof actualValue === "string" ||
|
|
156
|
+
typeof actualValue === "number" ||
|
|
157
|
+
typeof actualValue === "boolean") {
|
|
158
|
+
baseSchema = z.literal(actualValue);
|
|
159
|
+
}
|
|
160
|
+
else if (actualValue instanceof Date) {
|
|
161
|
+
baseSchema = z.date();
|
|
162
|
+
}
|
|
163
|
+
else if (actualValue === null) {
|
|
164
|
+
baseSchema = z.null();
|
|
165
|
+
}
|
|
166
|
+
else if (actualValue === undefined) {
|
|
167
|
+
baseSchema = z.undefined();
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
baseSchema = z.any();
|
|
171
|
+
}
|
|
172
|
+
// Apply the modifier
|
|
173
|
+
finalSchema = schemaOrModifier(baseSchema);
|
|
159
174
|
}
|
|
160
175
|
else {
|
|
161
|
-
|
|
176
|
+
// No schema provided, create from value type
|
|
177
|
+
if (typeof actualValue === "string" ||
|
|
178
|
+
typeof actualValue === "number" ||
|
|
179
|
+
typeof actualValue === "boolean") {
|
|
180
|
+
baseSchema = z.literal(actualValue);
|
|
181
|
+
}
|
|
182
|
+
else if (actualValue instanceof Date) {
|
|
183
|
+
baseSchema = z.date();
|
|
184
|
+
}
|
|
185
|
+
else if (actualValue === null) {
|
|
186
|
+
baseSchema = z.null();
|
|
187
|
+
}
|
|
188
|
+
else if (actualValue === undefined) {
|
|
189
|
+
baseSchema = z.undefined();
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
baseSchema = z.any();
|
|
193
|
+
}
|
|
194
|
+
finalSchema = baseSchema;
|
|
162
195
|
}
|
|
163
196
|
}
|
|
164
|
-
// Apply schema modifier if provided
|
|
165
|
-
const newSchema = isFunction(schemaModifier)
|
|
166
|
-
? schemaModifier(baseSchema)
|
|
167
|
-
: baseSchema;
|
|
168
197
|
const newCompletedStages = new Set(completedStages);
|
|
169
198
|
newCompletedStages.add("new");
|
|
170
199
|
// Create union for client/validation
|
|
171
|
-
const clientValidationSchema = z.union([config.sqlZod,
|
|
200
|
+
const clientValidationSchema = z.union([config.sqlZod, finalSchema]);
|
|
172
201
|
return createBuilder({
|
|
173
202
|
...config,
|
|
174
203
|
stage: "new",
|
|
175
|
-
newZod:
|
|
204
|
+
newZod: finalSchema,
|
|
176
205
|
initialValue: actualValue,
|
|
177
206
|
clientZod: clientValidationSchema,
|
|
178
207
|
validationZod: clientValidationSchema,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.119",
|
|
4
4
|
"description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|