Skip to content

Improve Zod + RHF Integration for input[type="number"] Coercion #2056

Open
@JoshuaS98

Description

@JoshuaS98

Description

When using input[type="number"] in a form, the DOM sets its value as a string, even though it represents a number. In React Hook Form (RHF), this value is passed as-is to the resolver.

However, when using Zod as the validation schema (via zodResolver), Zod expects a number type for something like z.number(), and it fails validation because it's receiving a string (e.g., "42" instead of 42).

The current workaround is to explicitly use z.coerce.number() instead of z.number(), which tells Zod to coerce the string into a number before validating.

The Zod resolver could optionally auto-coerce number-like strings (from number inputs) when validating against z.number().

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature 🚀New feature or requestneeds info ⏳Further information is required

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions