Skip to content

Commit

Permalink
fix: update init function to return output type and handle UUID gener…
Browse files Browse the repository at this point in the history
…ation for ZodString
  • Loading branch information
DenisBessa committed Jan 8, 2025
1 parent 5771c43 commit 9d7a1b7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clone from "just-clone";
import type { ZodTypeAny, input } from "zod";
import type { ZodTypeAny, input, output } from "zod";

export function init<T extends ZodTypeAny>(schema: T): input<T> {
export function init<T extends ZodTypeAny>(schema: T): output<T> {
const def = schema._def;
switch (def.typeName) {
case "ZodObject": {
Expand All @@ -13,8 +13,16 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
}
case "ZodRecord":
return {};
case "ZodString":
case "ZodString": {
if (def.checks) {
for (const check of def.checks) {
if (check.kind === "uuid") {
return crypto.randomUUID();
}
}
}
return "";
}
case "ZodNumber":
for (const check of def.checks || []) {
if (["min", "max"].includes(check.kind)) {
Expand Down

0 comments on commit 9d7a1b7

Please sign in to comment.