prisma-to-zod-v4 0.6.0 → 0.6.2
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 +2 -2
- package/bin/cli.js +1 -1
- package/dist/index.js +62 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -217,7 +217,7 @@ Generated code:
|
|
|
217
217
|
```ts
|
|
218
218
|
export const PostModel = z.object({
|
|
219
219
|
id: z.string().uuid(),
|
|
220
|
-
title: z.string().max(255, { message:
|
|
220
|
+
title: z.string().max(255, { message: "The title must be shorter than 256 characters" }),
|
|
221
221
|
contents: z.string().max(10240),
|
|
222
222
|
})
|
|
223
223
|
```
|
|
@@ -243,7 +243,7 @@ The referenced file can then be used by simply referring to exported members via
|
|
|
243
243
|
The generated zod schema files will now include a namespaced import like the following.
|
|
244
244
|
|
|
245
245
|
```typescript
|
|
246
|
-
import * as imports from
|
|
246
|
+
import * as imports from "../../src/zod-schemas"
|
|
247
247
|
```
|
|
248
248
|
|
|
249
249
|
#### Custom Zod Schema
|
package/bin/cli.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -213047,7 +213047,7 @@ Additional information: BADCLIENT: Bad error code, ${badCode} not found in range
|
|
|
213047
213047
|
});
|
|
213048
213048
|
|
|
213049
213049
|
// package.json
|
|
213050
|
-
var version = "0.6.
|
|
213050
|
+
var version = "0.6.2";
|
|
213051
213051
|
|
|
213052
213052
|
// src/index.ts
|
|
213053
213053
|
var import_generator_helper = require("@prisma/generator-helper");
|
|
@@ -213140,62 +213140,52 @@ var getZodConstructor = (field, getRelatedModelName = (name) => name.toString(),
|
|
|
213140
213140
|
switch (field.type) {
|
|
213141
213141
|
case "String":
|
|
213142
213142
|
zodType = "z.string()";
|
|
213143
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213144
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213145
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213146
|
-
const length = (_a = nativeType.match(/
|
|
213143
|
+
if (nativeType == null ? void 0 : nativeType.match(/^Uuid/)) zodType = "z.uuid()";
|
|
213144
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Citext/)) zodType = "z.string().toLowerCase()";
|
|
213145
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^VarChar\(\d+\)/)) {
|
|
213146
|
+
const length = (_a = nativeType.match(/VarChar\((\d+)\)/)) == null ? void 0 : _a[1];
|
|
213147
213147
|
if (length) extraModifiers.push(`max(${length})`);
|
|
213148
|
-
}
|
|
213149
|
-
|
|
213150
|
-
const length = (_b = nativeType.match(/VarChar\((\d+)\)/)) == null ? void 0 : _b[1];
|
|
213148
|
+
} else if (nativeType == null ? void 0 : nativeType.match(/^Char\(\d+\)/)) {
|
|
213149
|
+
const length = (_b = nativeType.match(/Char\((\d+)\)/)) == null ? void 0 : _b[1];
|
|
213151
213150
|
if (length) extraModifiers.push(`max(${length})`);
|
|
213152
|
-
}
|
|
213153
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213154
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213155
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213156
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213157
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213158
|
-
|
|
213159
|
-
const length = (_c = nativeType.match(/NChar\((\d+)\)/)) == null ? void 0 : _c[1];
|
|
213151
|
+
} else if (nativeType == null ? void 0 : nativeType.match(/^Text/)) zodType = "z.string()";
|
|
213152
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^VarBit/)) zodType = "z.string()";
|
|
213153
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Bit/)) zodType = "z.string()";
|
|
213154
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^VarBinary/)) zodType = "z.unknown()";
|
|
213155
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Binary/)) zodType = "z.unknown()";
|
|
213156
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^NVarChar\(\d+\)/)) {
|
|
213157
|
+
const length = (_c = nativeType.match(/NVarChar\((\d+)\)/)) == null ? void 0 : _c[1];
|
|
213160
213158
|
if (length) extraModifiers.push(`max(${length})`);
|
|
213161
|
-
}
|
|
213162
|
-
|
|
213163
|
-
const length = (_d = nativeType.match(/NVarChar\((\d+)\)/)) == null ? void 0 : _d[1];
|
|
213159
|
+
} else if (nativeType == null ? void 0 : nativeType.match(/^NChar\(\d+\)/)) {
|
|
213160
|
+
const length = (_d = nativeType.match(/NChar\((\d+)\)/)) == null ? void 0 : _d[1];
|
|
213164
213161
|
if (length) extraModifiers.push(`max(${length})`);
|
|
213165
|
-
}
|
|
213166
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213167
|
-
if (nativeType == null ? void 0 : nativeType.includes("ObjectId")) extraModifiers.push("regex(/^[0-9a-f]{24}$/i)");
|
|
213162
|
+
} else if (nativeType == null ? void 0 : nativeType.match(/^NText/)) zodType = "z.string()";
|
|
213163
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^ObjectId/)) extraModifiers.push("regex(/^[0-9a-f]{24}$/i)");
|
|
213168
213164
|
break;
|
|
213169
213165
|
case "Int":
|
|
213170
213166
|
zodType = "z.number()";
|
|
213171
213167
|
extraModifiers.push("int()");
|
|
213172
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213173
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213174
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213175
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213176
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213177
|
-
if (nativeType == null ? void 0 : nativeType.includes("Int")) zodType = "z.number().int()";
|
|
213168
|
+
if (nativeType == null ? void 0 : nativeType.match(/^SmallInt/)) extraModifiers.push("min(-32768)", "max(32767)");
|
|
213169
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^UnsignedInt/)) extraModifiers.push("min(0)");
|
|
213170
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^UnsignedSmallInt/)) extraModifiers.push("min(0)", "max(65535)");
|
|
213171
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^UnsignedTinyInt/)) extraModifiers.push("min(0)", "max(255)");
|
|
213172
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^TinyInt/)) extraModifiers.push("min(-128)", "max(127)");
|
|
213178
213173
|
break;
|
|
213179
213174
|
case "BigInt":
|
|
213180
213175
|
zodType = "z.bigint()";
|
|
213181
|
-
if (nativeType == null ? void 0 : nativeType.includes("BigInt")) zodType = "z.bigint()";
|
|
213182
213176
|
break;
|
|
213183
213177
|
case "Float":
|
|
213184
|
-
zodType = "z.number()";
|
|
213185
|
-
if (nativeType == null ? void 0 : nativeType.includes("Real")) zodType = "z.number()";
|
|
213186
|
-
if (nativeType == null ? void 0 : nativeType.includes("Float")) zodType = "z.number()";
|
|
213187
|
-
if (nativeType == null ? void 0 : nativeType.includes("Double")) zodType = "z.number()";
|
|
213178
|
+
zodType = "z.coerce.number()";
|
|
213188
213179
|
break;
|
|
213189
213180
|
case "Decimal":
|
|
213190
|
-
zodType = "z.number()";
|
|
213191
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213181
|
+
zodType = "z.coerce.number()";
|
|
213182
|
+
if (nativeType == null ? void 0 : nativeType.match(/^Numeric\(\d+,\d+\)/)) {
|
|
213192
213183
|
const match = nativeType.match(/Numeric\((\d+),(\d+)\)/);
|
|
213193
213184
|
if (match) {
|
|
213194
213185
|
const [, precision, scale] = match;
|
|
213195
213186
|
extraModifiers.push(`refine(x => /^\\d{1,${Number(precision) - parseInt(scale)}}(\\.\\d{1,${scale}})?$/.test(x.toString()))`);
|
|
213196
213187
|
}
|
|
213197
|
-
}
|
|
213198
|
-
if (nativeType == null ? void 0 : nativeType.includes("Decimal")) {
|
|
213188
|
+
} else if (nativeType == null ? void 0 : nativeType.match(/^Decimal\(\d+,\d+\)/)) {
|
|
213199
213189
|
const match = nativeType.match(/Decimal\((\d+),(\d+)\)/);
|
|
213200
213190
|
if (match) {
|
|
213201
213191
|
const [, precision, scale] = match;
|
|
@@ -213205,34 +213195,31 @@ var getZodConstructor = (field, getRelatedModelName = (name) => name.toString(),
|
|
|
213205
213195
|
break;
|
|
213206
213196
|
case "DateTime":
|
|
213207
213197
|
zodType = "z.date()";
|
|
213208
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213209
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213210
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213211
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213212
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213213
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213214
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213215
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213198
|
+
if (nativeType == null ? void 0 : nativeType.match(/^TimestampTz/)) zodType = "z.date()";
|
|
213199
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Timestamp/)) zodType = "z.date()";
|
|
213200
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^TimeTz/)) zodType = "z.string().regex(/^\\d{2}:\\d{2}:\\d{2}[+-]\\d{2}:\\d{2}$/)";
|
|
213201
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Time/)) zodType = "z.string().regex(/^\\d{2}:\\d{2}:\\d{2}$/)";
|
|
213202
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Date/)) zodType = "z.string().regex(/^\\d{4}-\\d{2}-\\d{2}$/)";
|
|
213203
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^DateTime/)) zodType = "z.date()";
|
|
213204
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^DateTimeOffset/)) zodType = "z.date()";
|
|
213205
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^DateTime2/)) zodType = "z.date()";
|
|
213216
213206
|
break;
|
|
213217
213207
|
case "Boolean":
|
|
213218
213208
|
zodType = "z.boolean()";
|
|
213219
213209
|
break;
|
|
213220
213210
|
case "Bytes":
|
|
213221
213211
|
zodType = "z.instanceof(Buffer)";
|
|
213222
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213223
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213224
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213225
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213226
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213227
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213228
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213229
|
-
if (nativeType == null ? void 0 : nativeType.
|
|
213212
|
+
if (nativeType == null ? void 0 : nativeType.match(/^ByteA/)) zodType = "z.instanceof(Buffer)";
|
|
213213
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^LongBlob/)) zodType = "z.instanceof(Buffer)";
|
|
213214
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^MediumBlob/)) zodType = "z.instanceof(Buffer)";
|
|
213215
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^TinyBlob/)) zodType = "z.instanceof(Buffer)";
|
|
213216
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Blob/)) zodType = "z.instanceof(Buffer)";
|
|
213217
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Image/)) zodType = "z.instanceof(Buffer)";
|
|
213218
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^VarBinary/)) zodType = "z.instanceof(Buffer)";
|
|
213219
|
+
else if (nativeType == null ? void 0 : nativeType.match(/^Binary/)) zodType = "z.instanceof(Buffer)";
|
|
213230
213220
|
break;
|
|
213231
213221
|
case "Json":
|
|
213232
213222
|
zodType = "jsonSchema";
|
|
213233
|
-
if (nativeType == null ? void 0 : nativeType.includes("Json")) zodType = "jsonSchema";
|
|
213234
|
-
if (nativeType == null ? void 0 : nativeType.includes("JsonB")) zodType = "jsonSchema";
|
|
213235
|
-
if (nativeType == null ? void 0 : nativeType.includes("Json")) zodType = "jsonSchema";
|
|
213236
213223
|
break;
|
|
213237
213224
|
}
|
|
213238
213225
|
} else if (field.kind === "enum") {
|
|
@@ -213381,7 +213368,12 @@ var writeImportsForModel = (model, sourceFile, config, { schemaPath, outputPath,
|
|
|
213381
213368
|
kind: import_ts_morph.StructureKind.ImportDeclaration,
|
|
213382
213369
|
moduleSpecifier: "./index",
|
|
213383
213370
|
namedImports: Array.from(
|
|
213384
|
-
new Set(
|
|
213371
|
+
new Set(
|
|
213372
|
+
filteredFields.flatMap((f) => [
|
|
213373
|
+
`Complete${f.type}`,
|
|
213374
|
+
relatedModelName(f.type)
|
|
213375
|
+
])
|
|
213376
|
+
)
|
|
213385
213377
|
)
|
|
213386
213378
|
});
|
|
213387
213379
|
}
|
|
@@ -213441,7 +213433,13 @@ var generateSchemaForModel = (model, sourceFile, config, { schemaPath }) => {
|
|
|
213441
213433
|
model.fields.filter((f) => f.kind !== "object").forEach((field) => {
|
|
213442
213434
|
writeArray(writer, getJSDocs(field.documentation));
|
|
213443
213435
|
const nativeType = modelNativeTypes.get(field.name);
|
|
213444
|
-
writer.write(
|
|
213436
|
+
writer.write(
|
|
213437
|
+
`${field.name}: ${getZodConstructor(
|
|
213438
|
+
field,
|
|
213439
|
+
void 0,
|
|
213440
|
+
nativeType
|
|
213441
|
+
)}`
|
|
213442
|
+
).write(",").newLine();
|
|
213445
213443
|
});
|
|
213446
213444
|
}).write(")");
|
|
213447
213445
|
}
|
|
@@ -213485,7 +213483,12 @@ var generateRelatedSchemaForModel = (model, sourceFile, config, _prismaOptions)
|
|
|
213485
213483
|
writer.write(`z.lazy(() => ${modelName(model.name)}.extend(`).inlineBlock(() => {
|
|
213486
213484
|
relationFields.forEach((field) => {
|
|
213487
213485
|
writeArray(writer, getJSDocs(field.documentation));
|
|
213488
|
-
writer.write(
|
|
213486
|
+
writer.write(
|
|
213487
|
+
`${field.name}: ${getZodConstructor(
|
|
213488
|
+
field,
|
|
213489
|
+
relatedModelName
|
|
213490
|
+
)}`
|
|
213491
|
+
).write(",").newLine();
|
|
213489
213492
|
});
|
|
213490
213493
|
}).write("))");
|
|
213491
213494
|
}
|