diff --git a/src/useFormo.ts b/src/useFormo.ts index c0940b1..477acf2 100644 --- a/src/useFormo.ts +++ b/src/useFormo.ts @@ -527,7 +527,7 @@ export function useFormo< subfieldName ]!; const result = (await subfieldValidation( - (values as V)[name][index][subfieldName] as any + (values as unknown as V)[name][index][subfieldName] as any )) as Result, Values[K]>; return matchResult(result, { failure: (e) => { @@ -560,7 +560,7 @@ export function useFormo< subfield: subfieldName, errors: undefined, }); - return success(values[name]); + return success((values as unknown as V)[name][index][subfieldName] as Values[K]); } } diff --git a/test/useFormo.test.ts b/test/useFormo.test.ts index cec93c4..ab7bb2a 100644 --- a/test/useFormo.test.ts +++ b/test/useFormo.test.ts @@ -229,6 +229,77 @@ describe("formo", () => { expect(onSubmit).toHaveBeenCalledWith({ city: "Rome" }); }); + test("onSubmit receives correct values with no validators", async () => { + const onSubmit = jest.fn(async () => success(null)); + + const { result } = renderHook(() => + useFormo( + { + initialValues: { + family: "Rossi", + persons: subFormValue([ + { + name: "Mario", + age: 25, + }, + ]), + }, + fieldValidators: (_) => ({}), + subFormValidators: (_) => ({}), + }, + { + onSubmit, + } + ) + ); + + await act(async () => { + result.current.handleSubmit(); + }); + + expect(onSubmit).toHaveBeenCalledTimes(1); + expect(onSubmit).toHaveBeenCalledWith({ family: "Rossi", persons: [{ name: "Mario", age: 25 }] }); + }); + + test("onSubmit receives correct values with validators", async () => { + const onSubmit = jest.fn(async () => success(null)); + + const { result } = renderHook(() => + useFormo( + { + initialValues: { + family: "Rossi", + persons: subFormValue([ + { + name: "Mario", + age: 25, + }, + ]), + }, + fieldValidators: (_) => ({ + family: validators.minLength(1, "required"), + }), + subFormValidators: (_) => ({ + persons: { + name: validators.minLength(1, "required"), + age: validators.fromPredicate((age) => age >= 0, "invalid age"), + }, + }), + }, + { + onSubmit, + } + ) + ); + + await act(async () => { + result.current.handleSubmit(); + }); + + expect(onSubmit).toHaveBeenCalledTimes(1); + expect(onSubmit).toHaveBeenCalledWith({ family: "Rossi", persons: [{ name: "Mario", age: 25 }] }); + }); + describe("field validations", () => { test("validation shows field error and prevents submit", async () => { const onSubmit = jest.fn(() => Promise.resolve(success(null)));