Skip to content

Fix subForm with no validators #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/useFormo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NonEmptyArray<FieldError>, Values[K]>;
return matchResult(result, {
failure: (e) => {
Expand Down Expand Up @@ -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]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as Values[K] is needed because the function expects it to be that.

function validateSubfield<
    SK extends keyof SubFormValues<Values> & string,
    K extends SubFormKeys<Values>,
    V extends ArrayRecord<Values, SK>
  >(
    name: K,
    index: number,
    subfieldName: SK,
    values: Values
  ): Promise<Result<NonEmptyArray<FieldError>, Values[K]>>

The return type should be something like "Promise<Result<NonEmptyArray<FieldError>, ArrayItem<Values[K]>[SK]>>" as that's what we're actually returning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah uhm, I see what you mean, Values[K] does not seem right (still it is being used and casted to in a lot of other places)

}
}

Expand Down
71 changes: 71 additions & 0 deletions test/useFormo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down