Skip to content

Programmatic form validation #14459

@Rich-Harris

Description

@Rich-Harris

Describe the problem

We now have declarative form validation...

export const myform = form(schema, (data) => {
  // `data` satisfies `schema`
});

...but there are some situations where validity can't be determined without attempting some action — e.g. if you check remaining stock inside the schema, the stock could change before the callback runs. It's unlikely, but possible.

Describe the proposed solution

Pass an invalid function as the second argument to the callback. This function receives an array of issues, and throws an error so that no further processing takes place (and, for example, the ongoing database transaction is rolled back).

The issues array can be an array of Standard Schema issues (i.e. produced with a validation library), or an array of { message: string, name?: string } objects. name must correspond to one of the form fields (TypeScript will enforce this); if omitted, the resulting issue will only exist in the top-level issues.$ array.

Whichever type of issue is used, they are all normalized to RemoteFormIssue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions