Skip to content
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

Type Definition file presents incorrect type for the custom validation function #157

Open
Eosis opened this issue Feb 18, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@Eosis
Copy link

Eosis commented Feb 18, 2022

While working on a form with a custom validation function, I encountered behaviour inconsistent with the type declaration file:
The validate function is typed as taking the entire form props as an argument, when in reality it only receives the latest updated value. See below:

export type FormProps<Inf = Record<string, unknown>> = {
context?: FormState;
initialValues?: Inf;
onSubmit?: ((values: Inf) => any) | ((values: Inf) => Promise<any>);
validate?: (values: Inf) => any | undefined;
validationSchema?: ObjectSchema<any>;
} & svelte.JSX.HTMLAttributes<HTMLFormElement>;

if (validateFunction) {
isValidating.set(true);
return Promise.resolve()
.then(() => validateFunction({[field]: value}))
.then((errs) =>
util.update(errors, field, !util.isNullish(errs) ? errs[field] : ''),
)
.finally(() => {
isValidating.set(false);
});
}

A quick fix would be to set the type of the argument to validate to be Partial<Inf>.

Longer term, it would probably be preferable to return the full type (all the values in the form) to the custom validate function, as this would allow behaviour such as ensuring validity of a form where two of the fields are linked, which currently is not possible.

@Eosis Eosis added the bug Something isn't working label Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant