Skip to content

Commit 8c6d0f1

Browse files
committed
Support ZodBranded
1 parent 20f1b21 commit 8c6d0f1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/index.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ test('Optional values', async () => {
219219
expect(output.errors).toStrictEqual({});
220220
});
221221

222+
test('Braded values', async () => {
223+
const schema = z.object({
224+
name: z.string().brand('name'),
225+
});
226+
227+
const output = await superValidate({ name: 'Name' }, schema);
228+
expect(output.valid).equals(true);
229+
expect(output.message).toBeUndefined();
230+
expect(output.data.name).equals('Name');
231+
expect(output.errors).toStrictEqual({});
232+
});
233+
222234
describe('Default values', () => {
223235
test('With a partial entity', async () => {
224236
const now = new Date();
@@ -801,7 +813,7 @@ test('Passthrough validation', async () => {
801813
});
802814
});
803815

804-
test.only('Preprocessed fields', async () => {
816+
test('Preprocessed fields', async () => {
805817
const schema = z.object({
806818
tristate: z.preprocess(
807819
(value) => (value === undefined ? undefined : Boolean(value)),

src/lib/superValidate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { traversePath } from './traversal.js';
2424
import type {
2525
z,
2626
AnyZodObject,
27+
ZodBranded,
28+
ZodTypeAny,
2729
ZodNumber,
2830
ZodLiteral,
2931
ZodNativeEnum,
@@ -194,7 +196,10 @@ function formDataToValidation<T extends AnyZodObject>(
194196
typeInfo: ZodTypeInfo
195197
): unknown {
196198
const newValue = valueOrDefault(value, false, true, typeInfo);
197-
const zodType = typeInfo.zodType;
199+
const zodType: ZodTypeAny =
200+
typeInfo.zodType._def.typeName === 'ZodBranded'
201+
? (typeInfo.zodType as ZodBranded<any, any>).unwrap()
202+
: typeInfo.zodType;
198203

199204
// If the value was empty, it now contains the default value,
200205
// so it can be returned immediately, unless it's boolean, which
@@ -531,7 +536,7 @@ export async function superValidate<
531536
| FormData
532537
| URLSearchParams
533538
| URL
534-
| Partial<z.infer<UnwrapEffects<T>>>
539+
| Partial<z.input<UnwrapEffects<T>>>
535540
| null
536541
| undefined,
537542
schema: T,

0 commit comments

Comments
 (0)