1.0.5 β€’ Published 6 months ago

react-zod-hook v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

React Zod Hook

A lightweight and flexible React hook for managing form state and validation using Zod.

Features

  • πŸš€ Easy to Use: Simple API for managing form state and validation.
  • πŸ”’ Type-Safe: Built with TypeScript for excellent type support.
  • πŸ› οΈ Customizable: Supports custom validation schemas using Zod.
  • πŸ“ Error Handling: Provides detailed error messages for invalid fields.

Installation

Install the library using npm:

npm install react-zod-hook

Or with yarn:

yarn add react-zod-hook

Usage

Basic Example

JavaScript

import React from "react";
import { useForm } from "react-zod-hook";
import { z } from "zod";

// Define a Zod schema for validation
const schema = z.object({
    name: z.string().min(1, "Name is required"),
    email: z.string().email("Invalid email"),
});

const MyForm = () => {
    const { data, onChange, onFormChange, validate, getError } = useForm({
        data: { name: "", email: "" },
        zodSchema: schema,
    });

    const handleSubmit = () => {
        if (validate()) {
            console.log("Form is valid:", data);
        } else {
            console.log("Form has errors");
        }
    };

    return (
        <form>
            <div>
                <label>Name:</label>
                <input
                    name="name"
                    value={data.name}
                    onChange={onFormChange}
                />
                {getError("name") && <p style={{ color: "red" }}>{getError("name")}</p>}
            </div>
            <div>
                <label>Email:</label>
                <input
                    name="email"
                    value={data.email}
                    onChange={onFormChange}
                />
                {getError("email") && <p style={{ color: "red" }}>{getError("email")}</p>}
            </div>
            <button type="button" onClick={handleSubmit}>
                Submit
            </button>
        </form>
    );
};

export default MyForm;

TypeScript

import React from "react";
import { useForm } from "react-zod-hook";
import { z } from "zod";

// Define a Zod schema for validation
const schema = z.object({
    name: z.string().min(1, "Name is required"),
    email: z.string().email("Invalid email"),
});

const MyForm: React.FC = () => {
    const { data, onChange, onFormChange, validate, getError } = useForm({
        data: { name: "", email: "" },
        zodSchema: schema,
    });

    const handleSubmit = () => {
        if (validate()) {
            console.log("Form is valid:", data);
        } else {
            console.log("Form has errors");
        }
    };

    return (
        <form>
            <div>
                <label>Name:</label>
                <input
                    name="name"
                    value={data.name}
                    onChange={onFormChange}
                />
                {getError("name") && <p style={{ color: "red" }}>{getError("name")}</p>}
            </div>
            <div>
                <label>Email:</label>
                <input
                    name="email"
                    value={data.email}
                    onChange={onFormChange}
                />
                {getError("email") && <p style={{ color: "red" }}>{getError("email")}</p>}
            </div>
            <button type="button" onClick={handleSubmit}>
                Submit
            </button>
        </form>
    );
};

export default MyForm;

API Reference

useForm Hook

Props

Prop NameTypeDescription
dataobjectInitial form data (e.g., { name: "", email: "" }).
zodSchemaZodSchemaA Zod schema for validation (e.g., z.object({ name: z.string() })).

Return Values

PropertyTypeDescription
dataobjectThe current form data.
onChange(name: string, value: any) => voidFunction to update a specific field in the form data.
onFormChange(event: React.ChangeEvent<HTMLInputElement>) => voidFunction to handle input change events.
validate() => booleanValidates the form data against the Zod schema. Returns true if valid.
getError(name: string) => stringReturns the validation error message for a specific field.
isLoadingbooleanIndicates if the form is in a loading state (useful for async operations).
errorValidationError[] | nullAn array of validation errors or null if no errors.
stateupdate(key: string, value: any) => voidFunction to update the internal state (e.g., isLoading, error).
setStateReact.Dispatch<React.SetStateAction<FormState>>Function to manually update the entire state.

Advanced Usage

Manually Update State

You can use the stateupdate function to manually update the internal state (e.g., isLoading, error):

const { stateupdate } = useForm({ data: {}, zodSchema: schema });

stateupdate("isLoading", true); // Set loading state
stateupdate("error", [{ for: "name", message: "Custom error" }]); // Set custom error

Use setState for Full Control

You can use the setState function to manually update the entire state:

const { setState } = useForm({ data: {}, zodSchema: schema });

setState((prev) => ({
    ...prev,
    isLoading: true,
    error: null,
}));

Validation Error Structure

The error property contains an array of validation errors with the following structure:

interface ValidationError {
    for: string; // Field name (e.g., "name", "email")
    message: string; // Error message (e.g., "Name is required")
}

Example Schema

Here’s an example of a more complex Zod schema:

const schema = z.object({
    name: z.string().min(1, "Name is required"),
    email: z.string().email("Invalid email"),
    age: z.number().min(18, "You must be at least 18 years old"),
    password: z.string().min(8, "Password must be at least 8 characters"),
});

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT

1.0.5

6 months ago

1.0.4

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago