Git Product home page Git Product logo

zodvalidator's Introduction

zodValidator

Overview

The zod-validator package provides a custom React hook, useZodState, which combines the power of React state management with the Zod validation library. This hook helps manage form state and validate inputs using Zod schemas, providing a simple way to handle form data and validation in React applications.

Installation

First, install the zod-validator package and its peer dependency, zod, using npm:

npm install @jazdevv/zod-validator zod

Usage

import { useZodState } from '@jazdevv/zod-validator';

import { z } from 'zod';

Defining a Zod Schema

Define a Zod schema that describes the structure and validation rules for your form data. For example:

const schema = z.object({

name: z.string().min(1, 'Name is required'),

age: z.number().min(18, 'Age must be at least 18'),

});

Using the useZodState Hook

Use the useZodState hook within your component to manage form state and validation:

`
function MyFormComponent() {

const { inputs, error, validate, setValues, setZodSchema } = useZodState({

initialValues: {

name: '',

age: '',

},

schema: schema,

});

  

const handleSubmit = (event) => {

event.preventDefault();

if (validate()) {

// Form is valid, proceed with submission

console.log('Form data:', inputs);

} else {

// Form is invalid, display errors

console.log('Validation errors:', error);

}

};

  

const handleChange = (event) => {

const { name, value } = event.target;

setValues({ [name]: value });

};

  

return (

<form onSubmit={handleSubmit}>

<div>

<label>

Name:

<input

type="text"

name="name"

value={inputs.name}

onChange={handleChange}

/>

</label>

{error.name && <span>{error.name}</span>}

</div>

<div>

<label>

Age:

<input

type="number"

name="age"

value={inputs.age}

onChange={handleChange}

/>

</label>

{error.age && <span>{error.age}</span>}

</div>

<button type="submit">Submit</button>

</form>

);

}

Parameters:

parameters (Object) description
initialValues (Object) An object representing the initial values of the form inputs.
schema (Zod Schema) A Zod schema object that defines the validation rules for the form.

Returns:

returned key desc
inputs (Object) The current state of the form inputs
error (Object) An object containing validation error messages.
validate (Function) A function that validates the form inputs against the Zod schema. Returns true if valid, false otherwise.
setValues (Function) A function to update the form inputs. Takes an object of key-value pairs to update the state.
setZodSchema (Function) A function to update the Zod schema used for validation.

Here is a complete example demonstrating how to use the useZodState hook in a React component:

import React from 'react';

import { useZodState } from '@jazdevv/zod-validator';

import { z } from 'zod';

  

const schema = z.object({

name: z.string().min(1, 'Name is required'),

age: z.number().min(18, 'Age must be at least 18'),

});

  

function MyFormComponent() {

    const { inputs, error, validate, setValues, setZodSchema } = useZodState({
    
    initialValues: {
    
    name: '',
    
    age: '',
    
    },
    
    schema: schema,
    
    });

  

    const handleSubmit = (event) => {
    
        event.preventDefault();
    
        if (validate()) {
    
            console.log('Form data:', inputs);
    
        } else {
    
            console.log('Validation errors:', error);
    
        }
    
    };

  

    const handleChange = (event) => {
    
        const { name, value } = event.target;
    
        setValues({ [name]: value });
    
    };

  

    return (
    
    <form  onSubmit={handleSubmit}>
    
        <div>
        
            <label> Name: </label>
            
            <input
            
                type="text"
                
                name="name"
                
                value={inputs.name}
                
                onChange={handleChange}
            
            />
            
            {error.name && <span>{error.name}</span>}
            
        </div>
            
        <div>
            
            <label> Age:</label>
            
            <input
            
                type="number"
                
                name="age"
                
                value={inputs.age}
                
                onChange={handleChange}
            
            />
            
            
            {error.age && <span>{error.age}</span>}
        
        </div>
        
        <button  type="submit">Submit</button>
    
    </form>
    
    );

}

  

export default MyFormComponent;

zodvalidator's People

Watchers

Jaz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.