From 5fdc7f9538f1fe25ba47c5f881588e79418e5b5c Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 28 Jan 2021 08:52:07 +0100 Subject: [PATCH] feat: update to react-hook-form v7 BREAKING CHANGE: Require react-hook-form >= 7 --- joi/src/__tests__/Form.tsx | 10 +++-- joi/src/__tests__/joi.ts | 26 +++++++++--- joi/src/joi.ts | 4 +- joi/src/types.ts | 7 ++-- package.json | 4 +- superstruct/src/__tests__/Form.tsx | 10 +++-- superstruct/src/__tests__/superstruct.ts | 13 ++++-- superstruct/src/superstruct.ts | 4 +- superstruct/src/types.ts | 7 ++-- vest/src/__tests__/Form.tsx | 10 +++-- vest/src/__tests__/vest.ts | 28 ++++++++++--- vest/src/types.ts | 7 ++-- vest/src/vest.ts | 4 +- yarn.lock | 8 ++-- yup/src/__tests__/Form.tsx | 10 +++-- yup/src/__tests__/yup.ts | 50 ++++++++++++++++++------ yup/src/types.ts | 7 ++-- yup/src/yup.ts | 4 +- zod/package.json | 2 +- zod/src/__tests__/Form.tsx | 10 +++-- zod/src/__tests__/zod.ts | 26 +++++++++--- zod/src/types.ts | 7 ++-- zod/src/zod.ts | 4 +- 23 files changed, 183 insertions(+), 79 deletions(-) diff --git a/joi/src/__tests__/Form.tsx b/joi/src/__tests__/Form.tsx index 7d6d0f41..3be8269a 100644 --- a/joi/src/__tests__/Form.tsx +++ b/joi/src/__tests__/Form.tsx @@ -20,16 +20,20 @@ interface Props { } function TestComponent({ onSubmit }: Props) { - const { register, errors, handleSubmit } = useForm({ + const { + register, + formState: { errors }, + handleSubmit, + } = useForm({ resolver: joiResolver(schema), // Useful to check TypeScript regressions }); return (
- + {errors.username && {errors.username.message}} - + {errors.password && {errors.password.message}} diff --git a/joi/src/__tests__/joi.ts b/joi/src/__tests__/joi.ts index d4ca6313..00bc7570 100644 --- a/joi/src/__tests__/joi.ts +++ b/joi/src/__tests__/joi.ts @@ -41,7 +41,7 @@ describe('joiResolver', () => { const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); const validateSpy = jest.spyOn(schema, 'validate'); - const result = await joiResolver(schema)(data); + const result = await joiResolver(schema)(data, undefined, { fields: {} }); expect(validateSpy).not.toHaveBeenCalled(); expect(validateAsyncSpy).toHaveBeenCalledTimes(1); @@ -62,7 +62,11 @@ describe('joiResolver', () => { const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); const validateSpy = jest.spyOn(schema, 'validate'); - const result = await joiResolver(schema, undefined, { mode: 'sync' })(data); + const result = await joiResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(validateAsyncSpy).not.toHaveBeenCalled(); expect(validateSpy).toHaveBeenCalledTimes(1); @@ -76,7 +80,7 @@ describe('joiResolver', () => { birthYear: 'birthYear', }; - const result = await joiResolver(schema)(data); + const result = await joiResolver(schema)(data, undefined, { fields: {} }); expect(result).toMatchSnapshot(); }); @@ -91,7 +95,11 @@ describe('joiResolver', () => { const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); const validateSpy = jest.spyOn(schema, 'validate'); - const result = await joiResolver(schema, undefined, { mode: 'sync' })(data); + const result = await joiResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(validateAsyncSpy).not.toHaveBeenCalled(); expect(validateSpy).toHaveBeenCalledTimes(1); @@ -105,7 +113,10 @@ describe('joiResolver', () => { birthYear: 'birthYear', }; - const result = await joiResolver(schema)(data, undefined, true); + const result = await joiResolver(schema)(data, undefined, { + fields: {}, + criteriaMode: 'all', + }); expect(result).toMatchSnapshot(); }); @@ -120,7 +131,10 @@ describe('joiResolver', () => { const result = await joiResolver(schema, undefined, { mode: 'sync' })( data, undefined, - true, + { + fields: {}, + criteriaMode: 'all', + }, ); expect(result).toMatchSnapshot(); diff --git a/joi/src/joi.ts b/joi/src/joi.ts index dd4985ca..7629b1d5 100644 --- a/joi/src/joi.ts +++ b/joi/src/joi.ts @@ -50,7 +50,7 @@ export const joiResolver: Resolver = ( abortEarly: false, }, { mode } = { mode: 'async' }, -) => async (values, _, validateAllFieldCriteria = false) => { +) => async (values, _, { criteriaMode }) => { try { let result; if (mode === 'async') { @@ -73,7 +73,7 @@ export const joiResolver: Resolver = ( return { values: {}, errors: transformToNestObject( - parseErrorSchema(e, validateAllFieldCriteria), + parseErrorSchema(e, criteriaMode === 'all'), ), }; } diff --git a/joi/src/types.ts b/joi/src/types.ts index c56a690d..11f04884 100644 --- a/joi/src/types.ts +++ b/joi/src/types.ts @@ -1,5 +1,6 @@ import { FieldValues, + ResolverOptions, ResolverResult, UnpackNestedValue, } from 'react-hook-form'; @@ -8,9 +9,9 @@ import type { AsyncValidationOptions, Schema } from 'joi'; export type Resolver = ( schema: T, schemaOptions?: AsyncValidationOptions, - resolverOptions?: { mode: 'async' | 'sync' }, + factoryOptions?: { mode: 'async' | 'sync' }, ) => ( values: UnpackNestedValue, - context?: TContext, - validateAllFieldCriteria?: boolean, + context: TContext | undefined, + options: ResolverOptions, ) => Promise>; diff --git a/package.json b/package.json index 7a39cc1d..ecbd8c1f 100644 --- a/package.json +++ b/package.json @@ -129,7 +129,7 @@ "prettier": "^2.2.1", "react": "^17.0.1", "react-dom": "^17.0.1", - "react-hook-form": "^6.14.2", + "react-hook-form": "^7.0.0-alpha.0", "semantic-release": "^17.3.6", "superstruct": "^0.13.3", "ts-jest": "^26.4.4", @@ -139,7 +139,7 @@ "zod": "^1.11.11" }, "peerDependencies": { - "react-hook-form": ">=6.6.0" + "react-hook-form": "^7.0.0" }, "husky": { "hooks": { diff --git a/superstruct/src/__tests__/Form.tsx b/superstruct/src/__tests__/Form.tsx index c1170fa2..7055d852 100644 --- a/superstruct/src/__tests__/Form.tsx +++ b/superstruct/src/__tests__/Form.tsx @@ -17,16 +17,20 @@ interface Props { } function TestComponent({ onSubmit }: Props) { - const { register, errors, handleSubmit } = useForm({ + const { + register, + formState: { errors }, + handleSubmit, + } = useForm({ resolver: superstructResolver(schema), // Useful to check TypeScript regressions }); return ( - + {errors.username && {errors.username.message}} - + {errors.password && {errors.password.message}} diff --git a/superstruct/src/__tests__/superstruct.ts b/superstruct/src/__tests__/superstruct.ts index 39c69f46..772a3d33 100644 --- a/superstruct/src/__tests__/superstruct.ts +++ b/superstruct/src/__tests__/superstruct.ts @@ -41,7 +41,9 @@ describe('superstructResolver', () => { enabled: true, }; - const result = await superstructResolver(schema)(data); + const result = await superstructResolver(schema)(data, undefined, { + fields: {}, + }); expect(result).toEqual({ errors: {}, values: data }); }); @@ -53,7 +55,9 @@ describe('superstructResolver', () => { birthYear: 'birthYear', }; - const result = await superstructResolver(schema)(data); + const result = await superstructResolver(schema)(data, undefined, { + fields: {}, + }); expect(result).toMatchSnapshot(); }); @@ -65,7 +69,10 @@ describe('superstructResolver', () => { birthYear: 'birthYear', }; - const result = await superstructResolver(schema)(data, undefined, true); + const result = await superstructResolver(schema)(data, undefined, { + fields: {}, + criteriaMode: 'all', + }); expect(result).toMatchSnapshot(); }); diff --git a/superstruct/src/superstruct.ts b/superstruct/src/superstruct.ts index ce4aed2e..4ca85383 100644 --- a/superstruct/src/superstruct.ts +++ b/superstruct/src/superstruct.ts @@ -43,7 +43,7 @@ const parseErrorSchema = ( export const superstructResolver: Resolver = (schema, options) => async ( values, _context, - validateAllFieldCriteria = false, + { criteriaMode }, ) => { const [errors, result] = validate(values, schema, options); @@ -51,7 +51,7 @@ export const superstructResolver: Resolver = (schema, options) => async ( return { values: {}, errors: transformToNestObject( - parseErrorSchema(errors, validateAllFieldCriteria), + parseErrorSchema(errors, criteriaMode === 'all'), ), }; } diff --git a/superstruct/src/types.ts b/superstruct/src/types.ts index 8cf3036e..d125e91d 100644 --- a/superstruct/src/types.ts +++ b/superstruct/src/types.ts @@ -1,5 +1,6 @@ import { FieldValues, + ResolverOptions, ResolverResult, UnpackNestedValue, } from 'react-hook-form'; @@ -9,9 +10,9 @@ type Options = Parameters[2]; export type Resolver = >( schema: T, - options?: Options, + factoryOtions?: Options, ) => ( values: UnpackNestedValue, - context?: TContext, - validateAllFieldCriteria?: boolean, + context: TContext | undefined, + options: ResolverOptions, ) => Promise>; diff --git a/vest/src/__tests__/Form.tsx b/vest/src/__tests__/Form.tsx index 7e3a05c9..ef833628 100644 --- a/vest/src/__tests__/Form.tsx +++ b/vest/src/__tests__/Form.tsx @@ -25,16 +25,20 @@ interface Props { } function TestComponent({ onSubmit }: Props) { - const { register, errors, handleSubmit } = useForm({ + const { + register, + formState: { errors }, + handleSubmit, + } = useForm({ resolver: vestResolver(validationSuite), // Useful to check TypeScript regressions }); return ( - + {errors.username && {errors.username.message}} - + {errors.password && {errors.password.message}} diff --git a/vest/src/__tests__/vest.ts b/vest/src/__tests__/vest.ts index 6f4f16d8..604e6512 100644 --- a/vest/src/__tests__/vest.ts +++ b/vest/src/__tests__/vest.ts @@ -40,7 +40,9 @@ describe('vestResolver', () => { data: 'test', }, }; - expect(await vestResolver(validationSuite)(data)).toEqual({ + expect( + await vestResolver(validationSuite)(data, undefined, { fields: {} }), + ).toEqual({ values: data, errors: {}, }); @@ -55,7 +57,11 @@ describe('vestResolver', () => { }, }; expect( - await vestResolver(validationSuite, undefined, { mode: 'sync' })(data), + await vestResolver(validationSuite, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ), ).toEqual({ values: data, errors: {}, @@ -71,7 +77,9 @@ describe('vestResolver', () => { }, }; - expect(await vestResolver(validationSuite)(data)).toMatchSnapshot(); + expect( + await vestResolver(validationSuite)(data, undefined, { fields: {} }), + ).toMatchSnapshot(); }); it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false and `mode: sync`', async () => { @@ -84,7 +92,11 @@ describe('vestResolver', () => { }; expect( - await vestResolver(validationSuite, undefined, { mode: 'sync' })(data), + await vestResolver(validationSuite, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ), ).toMatchSnapshot(); }); @@ -98,7 +110,11 @@ describe('vestResolver', () => { }; expect( - await vestResolver(validationSuite)(data, {}, true), + await vestResolver(validationSuite)( + data, + {}, + { fields: {}, criteriaMode: 'all' }, + ), ).toMatchSnapshot(); }); @@ -115,7 +131,7 @@ describe('vestResolver', () => { await vestResolver(validationSuite, undefined, { mode: 'sync' })( data, {}, - true, + { fields: {}, criteriaMode: 'all' }, ), ).toMatchSnapshot(); }); diff --git a/vest/src/types.ts b/vest/src/types.ts index 3f2fa03f..79a2ca69 100644 --- a/vest/src/types.ts +++ b/vest/src/types.ts @@ -1,5 +1,6 @@ import { FieldValues, + ResolverOptions, ResolverResult, UnpackNestedValue, } from 'react-hook-form'; @@ -10,11 +11,11 @@ export type ICreateResult = ReturnType; export type Resolver = ( schema: ICreateResult, schemaOptions?: never, - resolverOptions?: { mode: 'async' | 'sync' }, + factoryOptions?: { mode: 'async' | 'sync' }, ) => ( values: UnpackNestedValue, - context?: TContext, - validateAllFieldCriteria?: boolean, + context: TContext | undefined, + options: ResolverOptions, ) => Promise>; export type VestErrors = Record; diff --git a/vest/src/vest.ts b/vest/src/vest.ts index 6cf5f616..fcb995c7 100644 --- a/vest/src/vest.ts +++ b/vest/src/vest.ts @@ -32,7 +32,7 @@ export const vestResolver: Resolver = ( schema, _, { mode } = { mode: 'async' }, -) => async (values, _context, validateAllFieldCriteria = false) => { +) => async (values, _context, { criteriaMode }) => { let result: IVestResult | DraftResult; if (mode === 'async') { const validateSchema = promisify(schema); @@ -50,7 +50,7 @@ export const vestResolver: Resolver = ( return { values: {}, errors: transformToNestObject( - parseErrorSchema(errors, validateAllFieldCriteria), + parseErrorSchema(errors, criteriaMode === 'all'), ), }; }; diff --git a/yarn.lock b/yarn.lock index 716203ef..f874229a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8107,10 +8107,10 @@ react-dom@^17.0.1: object-assign "^4.1.1" scheduler "^0.20.1" -react-hook-form@^6.14.2: - version "6.14.2" - resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-6.14.2.tgz#60ab0b5367b220a3d96aa0b17bd0f13f9d8c326a" - integrity sha512-GgDUuT3Yfhl1BOcMl862uAFbCixSomtm3CVlQQ1qVu9Tq5BN2uUIRUIXP8l2Gy99eLUrBqU9x4E7N+si9cnvaw== +react-hook-form@^7.0.0-alpha.0: + version "7.0.0-alpha.0" + resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.0.0-alpha.0.tgz#e4d6f58308e99c9667c901809c4f729209f58257" + integrity sha512-p7zDRjVrDVRgrCiXyjbyWLdluenfDLWRT0xO9In5n9E8Kv2+J9mjULm7ElMcJ4+7pcfrz6vVQzTq/lSVxbAJqA== react-is@^17.0.1: version "17.0.1" diff --git a/yup/src/__tests__/Form.tsx b/yup/src/__tests__/Form.tsx index 6242da9f..bc1573e3 100644 --- a/yup/src/__tests__/Form.tsx +++ b/yup/src/__tests__/Form.tsx @@ -17,16 +17,20 @@ interface Props { } function TestComponent({ onSubmit }: Props) { - const { register, errors, handleSubmit } = useForm({ + const { + register, + formState: { errors }, + handleSubmit, + } = useForm({ resolver: yupResolver(schema), // Useful to check TypeScript regressions }); return ( - + {errors.username && {errors.username.message}} - + {errors.password && {errors.password.message}} diff --git a/yup/src/__tests__/yup.ts b/yup/src/__tests__/yup.ts index 7994d2e3..dec6d998 100644 --- a/yup/src/__tests__/yup.ts +++ b/yup/src/__tests__/yup.ts @@ -32,7 +32,7 @@ describe('yupResolver', () => { const schemaSpy = jest.spyOn(schema, 'validate'); const schemaSyncSpy = jest.spyOn(schema, 'validateSync'); - const result = await yupResolver(schema)(data); + const result = await yupResolver(schema)(data, undefined, { fields: {} }); expect(schemaSpy).toHaveBeenCalledTimes(1); expect(schemaSyncSpy).not.toHaveBeenCalled(); @@ -54,7 +54,11 @@ describe('yupResolver', () => { const validateSyncSpy = jest.spyOn(schema, 'validateSync'); const validateSpy = jest.spyOn(schema, 'validate'); - const result = await yupResolver(schema, undefined, { mode: 'sync' })(data); + const result = await yupResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(validateSyncSpy).toHaveBeenCalledTimes(1); expect(validateSpy).not.toHaveBeenCalled(); @@ -68,7 +72,7 @@ describe('yupResolver', () => { birthYear: 'birthYear', }; - const result = await yupResolver(schema)(data); + const result = await yupResolver(schema)(data, undefined, { fields: {} }); expect(result).toMatchSnapshot(); }); @@ -83,7 +87,11 @@ describe('yupResolver', () => { const validateSyncSpy = jest.spyOn(schema, 'validateSync'); const validateSpy = jest.spyOn(schema, 'validate'); - const result = await yupResolver(schema, undefined, { mode: 'sync' })(data); + const result = await yupResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(validateSyncSpy).toHaveBeenCalledTimes(1); expect(validateSpy).not.toHaveBeenCalled(); @@ -97,7 +105,10 @@ describe('yupResolver', () => { birthYear: 'birthYear', }; - const result = await yupResolver(schema)(data, undefined, true); + const result = await yupResolver(schema)(data, undefined, { + fields: {}, + criteriaMode: 'all', + }); expect(result).toMatchSnapshot(); }); @@ -112,7 +123,10 @@ describe('yupResolver', () => { const result = await yupResolver(schema, undefined, { mode: 'sync' })( data, undefined, - true, + { + fields: {}, + criteriaMode: 'all', + }, ); expect(result).toMatchSnapshot(); @@ -132,7 +146,9 @@ describe('yupResolver', () => { const validateSpy = jest.spyOn(schemaWithContext, 'validate'); - const result = await yupResolver(schemaWithContext)(data, context); + const result = await yupResolver(schemaWithContext)(data, context, { + fields: {}, + }); expect(validateSpy).toHaveBeenCalledTimes(1); expect(validateSpy).toHaveBeenCalledWith( data, @@ -153,7 +169,9 @@ describe('yupResolver', () => { inner: [{ message: 'error1', type: 'required' }], }); - const result = await yupResolver(yupSchema)({ name: '' }); + const result = await yupResolver(yupSchema)({ name: '' }, undefined, { + fields: {}, + }); expect(result).toMatchSnapshot(); }); @@ -161,7 +179,13 @@ describe('yupResolver', () => { jest.spyOn(console, 'warn').mockImplementation(jest.fn); process.env.NODE_ENV = 'development'; - await yupResolver(yup.object(), { context: { noContext: true } })({}); + await yupResolver(yup.object(), { context: { noContext: true } })( + {}, + undefined, + { + fields: {}, + }, + ); expect(console.warn).toHaveBeenCalledWith( "You should not used the yup options context. Please, use the 'useForm' context object instead", ); @@ -172,7 +196,11 @@ describe('yupResolver', () => { jest.spyOn(console, 'warn').mockImplementation(jest.fn); process.env.NODE_ENV = 'production'; - await yupResolver(yup.object(), { context: { noContext: true } })({}); + await yupResolver(yup.object(), { context: { noContext: true } })( + {}, + undefined, + { fields: {} }, + ); expect(console.warn).not.toHaveBeenCalled(); process.env.NODE_ENV = 'test'; }); @@ -189,7 +217,7 @@ describe('yupResolver', () => { 'Email or name are required', (value) => !!(value && (value.name || value.email)), ), - )({ name: '', email: '' }); + )({ name: '', email: '' }, undefined, { fields: {} }); expect(result).toMatchSnapshot(); }); diff --git a/yup/src/types.ts b/yup/src/types.ts index f72b7cc9..47975007 100644 --- a/yup/src/types.ts +++ b/yup/src/types.ts @@ -1,5 +1,6 @@ import { FieldValues, + ResolverOptions, ResolverResult, UnpackNestedValue, } from 'react-hook-form'; @@ -10,9 +11,9 @@ type Options = Parameters[1]; export type Resolver = ( schema: T, schemaOptions?: Options, - resolverOptions?: { mode: 'async' | 'sync' }, + factoryOptions?: { mode: 'async' | 'sync' }, ) => ( values: UnpackNestedValue, - context?: TContext, - validateAllFieldCriteria?: boolean, + context: TContext | undefined, + options: ResolverOptions, ) => Promise>; diff --git a/yup/src/yup.ts b/yup/src/yup.ts index ccc5668e..4af51514 100644 --- a/yup/src/yup.ts +++ b/yup/src/yup.ts @@ -60,7 +60,7 @@ export const yupResolver: Resolver = ( abortEarly: false, }, { mode } = { mode: 'async' }, -) => async (values, context, validateAllFieldCriteria = false) => { +) => async (values, context, { criteriaMode }) => { try { if (options.context && process.env.NODE_ENV === 'development') { // eslint-disable-next-line no-console @@ -85,7 +85,7 @@ export const yupResolver: Resolver = ( errors: {}, }; } catch (e) { - const parsedErrors = parseErrorSchema(e, validateAllFieldCriteria); + const parsedErrors = parseErrorSchema(e, criteriaMode === 'all'); return { values: {}, diff --git a/zod/package.json b/zod/package.json index 715d5571..01c1bdb1 100644 --- a/zod/package.json +++ b/zod/package.json @@ -11,7 +11,7 @@ "types": "dist/index.d.ts", "license": "MIT", "peerDependencies": { - "react-hook-form": ">=6.6.0", + "react-hook-form": "^7.0.0", "@hookform/resolvers": "^1.0.0" } } diff --git a/zod/src/__tests__/Form.tsx b/zod/src/__tests__/Form.tsx index 78009ebc..715349c0 100644 --- a/zod/src/__tests__/Form.tsx +++ b/zod/src/__tests__/Form.tsx @@ -17,16 +17,20 @@ interface Props { } function TestComponent({ onSubmit }: Props) { - const { register, errors, handleSubmit } = useForm({ + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ resolver: zodResolver(schema), // Useful to check TypeScript regressions }); return ( - + {errors.username && {errors.username.message}} - + {errors.password && {errors.password.message}} diff --git a/zod/src/__tests__/zod.ts b/zod/src/__tests__/zod.ts index eaa5de69..c9e4686d 100644 --- a/zod/src/__tests__/zod.ts +++ b/zod/src/__tests__/zod.ts @@ -31,7 +31,7 @@ describe('zodResolver', () => { const parseAsyncSpy = jest.spyOn(schema, 'parseAsync'); - const result = await zodResolver(schema)(data); + const result = await zodResolver(schema)(data, undefined, { fields: {} }); expect(parseAsyncSpy).toHaveBeenCalledTimes(1); expect(result).toEqual({ errors: {}, values: data }); @@ -51,7 +51,11 @@ describe('zodResolver', () => { const parseSpy = jest.spyOn(schema, 'parse'); const parseAsyncSpy = jest.spyOn(schema, 'parseAsync'); - const result = await zodResolver(schema, undefined, { mode: 'sync' })(data); + const result = await zodResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(parseSpy).toHaveBeenCalledTimes(1); expect(parseAsyncSpy).not.toHaveBeenCalled(); @@ -65,7 +69,7 @@ describe('zodResolver', () => { birthYear: 'birthYear', }; - const result = await zodResolver(schema)(data); + const result = await zodResolver(schema)(data, undefined, { fields: {} }); expect(result).toMatchSnapshot(); }); @@ -80,7 +84,11 @@ describe('zodResolver', () => { const parseSpy = jest.spyOn(schema, 'parse'); const parseAsyncSpy = jest.spyOn(schema, 'parseAsync'); - const result = await zodResolver(schema, undefined, { mode: 'sync' })(data); + const result = await zodResolver(schema, undefined, { mode: 'sync' })( + data, + undefined, + { fields: {} }, + ); expect(parseSpy).toHaveBeenCalledTimes(1); expect(parseAsyncSpy).not.toHaveBeenCalled(); @@ -94,7 +102,10 @@ describe('zodResolver', () => { birthYear: 'birthYear', }; - const result = await zodResolver(schema)(data, undefined, true); + const result = await zodResolver(schema)(data, undefined, { + fields: {}, + criteriaMode: 'all', + }); expect(result).toMatchSnapshot(); }); @@ -109,7 +120,10 @@ describe('zodResolver', () => { const result = await zodResolver(schema, undefined, { mode: 'sync' })( data, undefined, - true, + { + fields: {}, + criteriaMode: 'all', + }, ); expect(result).toMatchSnapshot(); diff --git a/zod/src/types.ts b/zod/src/types.ts index f51f3230..3126ba03 100644 --- a/zod/src/types.ts +++ b/zod/src/types.ts @@ -2,6 +2,7 @@ import { FieldValues, ResolverResult, UnpackNestedValue, + ResolverOptions, } from 'react-hook-form'; import * as z from 'zod'; import type { ParseParams } from 'zod/lib/src/parser'; @@ -9,9 +10,9 @@ import type { ParseParams } from 'zod/lib/src/parser'; export type Resolver = >( schema: T, schemaOptions?: ParseParams, - resolverOptions?: { mode: 'async' | 'sync' }, + factoryOptions?: { mode: 'async' | 'sync' }, ) => ( values: UnpackNestedValue, - context?: TContext, - validateAllFieldCriteria?: boolean, + context: TContext | undefined, + options: ResolverOptions, ) => Promise>; diff --git a/zod/src/zod.ts b/zod/src/zod.ts index 56b0ca24..02e7f320 100644 --- a/zod/src/zod.ts +++ b/zod/src/zod.ts @@ -51,7 +51,7 @@ export const zodResolver: Resolver = ( schema, schemaOptions, { mode } = { mode: 'async' }, -) => async (values, _, validateAllFieldCriteria = false) => { +) => async (values, _, { criteriaMode }) => { try { const result = mode === 'async' @@ -63,7 +63,7 @@ export const zodResolver: Resolver = ( return { values: {}, errors: transformToNestObject( - parseErrorSchema(error, validateAllFieldCriteria), + parseErrorSchema(error, criteriaMode === 'all'), ), }; }