-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8beb9b
commit fddae77
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
# zod-forms | ||
[![npm version](https://shields.io/npm/v/zod-forms.svg)](https://www.npmjs.com/package/zod-forms) | ||
[![npm downloads](https://shields.io/npm/dm/zod-forms.svg)](https://www.npmjs.com/package/zod-forms) | ||
[![license](https://shields.io/npm/l/zod-forms.svg)](https://www.npmjs.com/package/zod-forms) | ||
|
||
|
||
Zod Forms is a library for building forms in React using [Zod](https://zod.dev/) for validation. | ||
|
||
# Usage | ||
To use `zod-forms` you need to create a form schema using Zod. The schema is a plain object with keys corresponding to the form fields and values corresponding to the validation rules. For example: | ||
```typescript | ||
import z from 'zod'; | ||
|
||
export const demoSchema = z.object({ | ||
name: z | ||
.string() | ||
.nonempty('Name is required') | ||
.min(2, {message: 'Must be 2 or more characters long'}), | ||
age: z.number(), | ||
}); | ||
|
||
``` | ||
For more information on Zod validation rules see the [Zod documentation](https://zod.dev/?id=basic-usage). | ||
|
||
You can then use the `useZodForm` hook to create a form component. | ||
Here is a simple example: | ||
```typescript jsx | ||
import React from 'react'; | ||
import {useZodForm} from 'zod-forms'; | ||
import {demoSchema} from './DemoSchema'; | ||
|
||
export function TestComponent() { | ||
const {Form} = useZodForm(demoSchema); | ||
|
||
return ( | ||
<div> | ||
<Form.Fields.name.Input> | ||
<input placeholder="Your Name" /> | ||
</Form.Fields.name.Input> | ||
<Form.Fields.age.Input> | ||
<input placeholder="Your Age" /> | ||
</Form.Fields.age.Input> | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters