diff --git a/zod/src/__tests__/__snapshots__/zod.ts.snap b/zod/src/__tests__/__snapshots__/zod.ts.snap index 1bb824df..ab468fff 100644 --- a/zod/src/__tests__/__snapshots__/zod.ts.snap +++ b/zod/src/__tests__/__snapshots__/zod.ts.snap @@ -40,6 +40,46 @@ Object { } `; +exports[`zodResolver should return a single error from zodResolver with \`mode: sync\` when validation fails 1`] = ` +Object { + "errors": Object { + "birthYear": Object { + "message": "Invalid input", + "type": "invalid_union", + }, + "confirm": Object { + "message": "Passwords don't match", + "type": "custom_error", + }, + "email": Object { + "message": "Invalid email", + "type": "invalid_string", + }, + "enabled": Object { + "message": "Required", + "type": "invalid_type", + }, + "password": Object { + "message": "Invalid", + "type": "invalid_string", + }, + "repeatPassword": Object { + "message": "Required", + "type": "invalid_type", + }, + "tags": Object { + "message": "Required", + "type": "invalid_type", + }, + "username": Object { + "message": "Required", + "type": "invalid_type", + }, + }, + "values": Object {}, +} +`; + exports[`zodResolver should return all the errors from zodResolver when validation fails with \`validateAllFieldCriteria\` set to true 1`] = ` Object { "errors": Object { diff --git a/zod/src/__tests__/zod.ts b/zod/src/__tests__/zod.ts index b83c44d1..50d9704b 100644 --- a/zod/src/__tests__/zod.ts +++ b/zod/src/__tests__/zod.ts @@ -34,6 +34,22 @@ describe('zodResolver', () => { expect(result).toEqual({ errors: {}, values: data }); }); + it('should return values from zodResolver with `mode: sync` when validation pass', async () => { + const data: z.infer = { + username: 'Doe', + password: 'Password123', + repeatPassword: 'Password123', + birthYear: 2000, + email: 'john@doe.com', + tags: ['tag1', 'tag2'], + enabled: true, + }; + + const result = await zodResolver(schema, undefined, { mode: 'sync' })(data); + + expect(result).toEqual({ errors: {}, values: data }); + }); + it('should return a single error from zodResolver when validation fails', async () => { const data = { password: '___', @@ -46,6 +62,18 @@ describe('zodResolver', () => { expect(result).toMatchSnapshot(); }); + it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => { + const data = { + password: '___', + email: '', + birthYear: 'birthYear', + }; + + const result = await zodResolver(schema, undefined, { mode: 'sync' })(data); + + expect(result).toMatchSnapshot(); + }); + it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => { const data = { password: '___', diff --git a/zod/src/types.ts b/zod/src/types.ts index 9dc4e9e7..f51f3230 100644 --- a/zod/src/types.ts +++ b/zod/src/types.ts @@ -8,7 +8,8 @@ import type { ParseParams } from 'zod/lib/src/parser'; export type Resolver = >( schema: T, - options?: ParseParams, + schemaOptions?: ParseParams, + resolverOptions?: { mode: 'async' | 'sync' }, ) => ( values: UnpackNestedValue, context?: TContext, diff --git a/zod/src/zod.ts b/zod/src/zod.ts index 7a7d528a..56b0ca24 100644 --- a/zod/src/zod.ts +++ b/zod/src/zod.ts @@ -47,21 +47,24 @@ const parseErrorSchema = ( ); }; -export const zodResolver: Resolver = (schema, options) => async ( - values, - _, - validateAllFieldCriteria = false, -) => { - const result = schema.safeParse(values, options); +export const zodResolver: Resolver = ( + schema, + schemaOptions, + { mode } = { mode: 'async' }, +) => async (values, _, validateAllFieldCriteria = false) => { + try { + const result = + mode === 'async' + ? await schema.parseAsync(values, schemaOptions) + : schema.parse(values, schemaOptions); - if (result.success) { - return { values: result.data, errors: {} }; + return { values: result, errors: {} }; + } catch (error) { + return { + values: {}, + errors: transformToNestObject( + parseErrorSchema(error, validateAllFieldCriteria), + ), + }; } - - return { - values: {}, - errors: transformToNestObject( - parseErrorSchema(result.error, validateAllFieldCriteria), - ), - }; };