Skip to content

Commit 7f3aad5

Browse files
committed
More improvements
1 parent 9ba508c commit 7f3aad5

File tree

1 file changed

+31
-13
lines changed
  • packages/convex-helpers/server

1 file changed

+31
-13
lines changed

packages/convex-helpers/server/zod.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export type CustomBuilder<
507507
ReturnsZodValidator extends z.ZodTypeAny | ZodValidator | void = void,
508508
ReturnValue extends ReturnValueInput<ReturnsZodValidator> = any,
509509
// Note: this differs from customFunctions.ts b/c we don't need to track
510-
// the exact args to match the standard builder types. For zod we don't
510+
// the exact args to match the standard builder types. For Zod we don't
511511
// try to ever pass a custom function as a builder to another custom
512512
// function, so we can be looser here.
513513
>(
@@ -597,7 +597,9 @@ type ConvexObjectValidatorFromZod<T extends ZodValidator> = VObject<
597597
* Converts a Zod validator type
598598
* to the corresponding Convex validator type from `convex/values`.
599599
*
600-
* For instance, `ConvexValidatorFromZod<Z.ZodString>` evaluates to `VString`.
600+
* ```ts
601+
* ConvexValidatorFromZod<z.ZodString> // → VString
602+
* ```
601603
*/
602604
type ConvexValidatorFromZod<Z extends z.ZodTypeAny> =
603605
// Keep this in sync with zodToConvex implementation
@@ -963,7 +965,7 @@ export function zodToConvex<Z extends z.ZodTypeAny>(
963965
case "ZodPipeline":
964966
return zodToConvex(zod._def.in) as ConvexValidatorFromZod<Z>;
965967
default:
966-
throw new Error(`Unknown zod type: ${typeName}`);
968+
throw new Error(`Unknown Zod type: ${typeName}`);
967969
// N/A or not supported
968970
// case "ZodDate":
969971
// case "ZodSymbol":
@@ -981,8 +983,12 @@ export function zodToConvex<Z extends z.ZodTypeAny>(
981983
}
982984

983985
/**
984-
* This is the type of a convex validator that checks the value *after* it has
985-
* been validated (and possibly transformed) by a zod validator.
986+
* This is the type of a Convex validator that checks the value *after* it has
987+
* been validated (and possibly transformed) by a Zod validator.
988+
*
989+
* The difference between {@link ConvexValidatorFromZod}
990+
* and `ConvexValidatorFromZodOutput` are explained in the documentation of
991+
* {@link zodToConvex}/{@link zodOutputToConvex}.
986992
*/
987993
export type ConvexValidatorFromZodOutput<Z extends z.ZodTypeAny> =
988994
// Keep this in sync with the zodOutputToConvex implementation
@@ -1430,7 +1436,10 @@ export const withSystemFields = <
14301436
return { ...zObject, _id: zid(tableName), _creationTime: z.number() };
14311437
};
14321438

1433-
// This is a copy of zod's ZodBranded which also brands the input.
1439+
/**
1440+
* This is a copy of Zod’s `ZodBranded` which also brands the input
1441+
* (see {@link zBrand})
1442+
*/
14341443
export class ZodBrandedInputAndOutput<
14351444
T extends z.ZodTypeAny,
14361445
B extends string | number | symbol,
@@ -1471,14 +1480,14 @@ export function zBrand<
14711480
return validator.brand(brand);
14721481
}
14731482

1474-
/** Simple type conversion from a Convex validator to a Zod validator. */
1475-
export type ConvexToZod<V extends GenericValidator> = z.ZodType<Infer<V>>;
1476-
1477-
/** Better type conversion from a Convex validator to a Zod validator where the output is not a generetic ZodType but it's more specific.
1483+
/**
1484+
* Simple type conversion from a Convex validator to a Zod validator.
14781485
*
1479-
* ES: z.ZodString instead of z.ZodType<string, z.ZodTypeDef, string>
1480-
* so you can use methods of z.ZodString like .min() or .email()
1486+
* ```ts
1487+
* ConvexToZod<typeof v.string()> // → z.ZodType<string>
1488+
* ```
14811489
*/
1490+
export type ConvexToZod<V extends GenericValidator> = z.ZodType<Infer<V>>;
14821491

14831492
type ZodFromValidatorBase<V extends GenericValidator> =
14841493
V extends VId<GenericId<infer TableName extends string>>
@@ -1536,7 +1545,16 @@ type ZodFromValidatorBase<V extends GenericValidator> =
15361545
>
15371546
: z.ZodTypeAny; // fallback for unknown validators
15381547

1539-
/** Main type with optional handling. */
1548+
/**
1549+
* Better type conversion from a Convex validator to a Zod validator
1550+
* where the output is not a generic ZodType but it's more specific.
1551+
*
1552+
* This allows you to use methods specific to the Zod type (e.g. `.email()` for `z.ZodString).
1553+
*
1554+
* ```ts
1555+
* ZodFromValidatorBase<typeof v.string()> // → z.ZodString
1556+
* ```
1557+
*/
15401558
export type ZodValidatorFromConvex<V extends GenericValidator> =
15411559
V extends Validator<any, "optional", any>
15421560
? z.ZodOptional<ZodFromValidatorBase<V>>

0 commit comments

Comments
 (0)