convex-helpers
Advanced tools
Comparing version 0.1.65 to 0.1.66-alpha.0
{ | ||
"name": "convex-helpers", | ||
"version": "0.1.65", | ||
"version": "0.1.66-alpha.0", | ||
"description": "A collection of useful code to complement the official convex package.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -228,3 +228,3 @@ import { ZodTypeDef, z } from "zod"; | ||
VNull | ||
], "optional", ConvexValidatorFromZod<Inner>["fieldPaths"]> : never : Z extends z.ZodBranded<infer Inner, any> ? ConvexValidatorFromZod<Inner> : Z extends z.ZodDefault<infer Inner> ? ConvexValidatorFromZod<Inner> extends GenericValidator ? VOptional<ConvexValidatorFromZod<Inner>> : never : Z extends z.ZodRecord<infer K, infer V> ? K extends z.ZodString | Zid<string> | z.ZodUnion<[ | ||
], "optional", ConvexValidatorFromZod<Inner>["fieldPaths"]> : never : Z extends z.ZodBranded<infer Inner, infer Brand> | ZodBrandedInputAndOutput<infer Inner, infer Brand> ? Inner extends z.ZodString ? VString<string & z.BRAND<Brand>> : Inner extends z.ZodNumber ? VFloat64<number & z.BRAND<Brand>> : Inner extends z.ZodBigInt ? VInt64<bigint & z.BRAND<Brand>> : ConvexValidatorFromZod<Inner> : Z extends z.ZodDefault<infer Inner> ? ConvexValidatorFromZod<Inner> extends GenericValidator ? VOptional<ConvexValidatorFromZod<Inner>> : never : Z extends z.ZodRecord<infer K, infer V> ? K extends z.ZodString | Zid<string> | z.ZodUnion<[ | ||
(z.ZodString | Zid<string>), | ||
@@ -268,3 +268,8 @@ (z.ZodString | Zid<string>), | ||
}; | ||
export declare class ZodBrandedInputAndOutput<T extends z.ZodTypeAny, B extends string | number | symbol> extends z.ZodType<T["_output"] & z.BRAND<B>, z.ZodBrandedDef<T>, T["_input"] & z.BRAND<B>> { | ||
_parse(input: z.ParseInput): z.ParseReturnType<any>; | ||
unwrap(): T; | ||
} | ||
export declare function zBrand<T extends z.ZodTypeAny, B extends string | number | symbol>(validator: T, brand?: B): ZodBrandedInputAndOutput<T, B>; | ||
export {}; | ||
//# sourceMappingURL=zod.d.ts.map |
@@ -355,1 +355,19 @@ import { z } from "zod"; | ||
}; | ||
// This is a copy of zod's ZodBranded which also brands the input. | ||
export class ZodBrandedInputAndOutput extends z.ZodType { | ||
_parse(input) { | ||
const { ctx } = this._processInputParams(input); | ||
const data = ctx.data; | ||
return this._def.type._parse({ | ||
data, | ||
path: ctx.path, | ||
parent: ctx, | ||
}); | ||
} | ||
unwrap() { | ||
return this._def.type; | ||
} | ||
} | ||
export function zBrand(validator, brand) { | ||
return validator.brand(brand); | ||
} |
@@ -14,5 +14,5 @@ import { | ||
import { modules } from "./setup.test.js"; | ||
import { zCustomQuery, zid, zodToConvexFields } from "./zod.js"; | ||
import { zBrand, zCustomQuery, zid, zodToConvexFields } from "./zod.js"; | ||
import { customCtx } from "./customFunctions.js"; | ||
import { v } from "convex/values"; | ||
import { v, VString } from "convex/values"; | ||
import { z } from "zod"; | ||
@@ -62,3 +62,4 @@ | ||
nullable: z.nullable(z.string()), | ||
branded: z.string().brand("branded"), | ||
// z.string().brand("branded") also works, but zBrand also brands the input | ||
branded: zBrand(z.string(), "branded"), | ||
default: z.string().default("default"), | ||
@@ -322,3 +323,3 @@ readonly: z.object({ a: z.string(), b: z.number() }).readonly(), | ||
nullable: null, | ||
branded: "branded", | ||
branded: "branded" as string & z.BRAND<"branded">, | ||
default: undefined, | ||
@@ -593,3 +594,3 @@ readonly: { a: "1", b: 2 }, | ||
literal: v.literal("hi"), | ||
branded: v.string(), | ||
branded: v.string() as VString<string & z.BRAND<"branded">>, | ||
}, | ||
@@ -653,2 +654,22 @@ ), | ||
); | ||
// Validate that our double-branded type is correct. | ||
assert( | ||
sameType( | ||
zodToConvexFields({ | ||
branded2: zBrand(z.string(), "branded2"), | ||
}), | ||
{ | ||
branded2: v.string() as VString<string & z.BRAND<"branded2">>, | ||
}, | ||
), | ||
); | ||
const s = zBrand(z.string(), "brand"); | ||
const n = zBrand(z.number(), "brand"); | ||
const i = zBrand(z.bigint(), "brand"); | ||
assert(true as Equals<z.input<typeof s>, string & z.BRAND<"brand">>); | ||
assert(true as Equals<z.output<typeof s>, string & z.BRAND<"brand">>); | ||
assert(true as Equals<z.input<typeof n>, number & z.BRAND<"brand">>); | ||
assert(true as Equals<z.output<typeof n>, number & z.BRAND<"brand">>); | ||
assert(true as Equals<z.input<typeof i>, bigint & z.BRAND<"brand">>); | ||
assert(true as Equals<z.output<typeof i>, bigint & z.BRAND<"brand">>); | ||
@@ -655,0 +676,0 @@ function sameType<T, U>(_t: T, _u: U): Equals<T, U> { |
@@ -574,7 +574,22 @@ import { ZodFirstPartyTypeKind, ZodTypeDef, z } from "zod"; | ||
: never | ||
: Z extends z.ZodBranded< | ||
infer Inner, | ||
any | ||
> | ||
? ConvexValidatorFromZod<Inner> | ||
: Z extends | ||
| z.ZodBranded< | ||
infer Inner, | ||
infer Brand | ||
> | ||
| ZodBrandedInputAndOutput< | ||
infer Inner, | ||
infer Brand | ||
> | ||
? Inner extends z.ZodString | ||
? VString<string & z.BRAND<Brand>> | ||
: Inner extends z.ZodNumber | ||
? VFloat64< | ||
number & z.BRAND<Brand> | ||
> | ||
: Inner extends z.ZodBigInt | ||
? VInt64< | ||
bigint & z.BRAND<Brand> | ||
> | ||
: ConvexValidatorFromZod<Inner> | ||
: Z extends z.ZodDefault< | ||
@@ -816,1 +831,31 @@ infer Inner | ||
}; | ||
// This is a copy of zod's ZodBranded which also brands the input. | ||
export class ZodBrandedInputAndOutput< | ||
T extends z.ZodTypeAny, | ||
B extends string | number | symbol, | ||
> extends z.ZodType< | ||
T["_output"] & z.BRAND<B>, | ||
z.ZodBrandedDef<T>, | ||
T["_input"] & z.BRAND<B> | ||
> { | ||
_parse(input: z.ParseInput) { | ||
const { ctx } = this._processInputParams(input); | ||
const data = ctx.data; | ||
return this._def.type._parse({ | ||
data, | ||
path: ctx.path, | ||
parent: ctx, | ||
}); | ||
} | ||
unwrap() { | ||
return this._def.type; | ||
} | ||
} | ||
export function zBrand< | ||
T extends z.ZodTypeAny, | ||
B extends string | number | symbol, | ||
>(validator: T, brand?: B): ZodBrandedInputAndOutput<T, B> { | ||
return validator.brand(brand); | ||
} |
Sorry, the diff of this file is not supported yet
852073
19595