@liam-michel/validated-form 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Liam Michel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # @liam-michel/validated-form
2
+
3
+ [![CI](https://github.com/liam-michel/validated-form/actions/workflows/ci.yml/badge.svg)](https://github.com/liam-michel/validated-form/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![npm version](https://img.shields.io/npm/v/@liam-michel/validated-form)](https://www.npmjs.com/package/@liam-michel/validated-form)
6
+ [![npm downloads](https://img.shields.io/npm/dm/@liam-michel/validated-form)](https://www.npmjs.com/package/@liam-michel/validated-form)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5-blue.svg)](https://www.typescriptlang.org/)
8
+
9
+ A type-safe, schema-driven form library for React. Define a [Zod](https://zod.dev) schema and get validated, fully-typed form fields out of the box — built on [React Hook Form](https://react-hook-form.com) and [Radix UI](https://www.radix-ui.com).
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @liam-michel/validated-form
15
+ ```
16
+
17
+ ### Peer dependencies
18
+
19
+ You'll also need these in your project:
20
+
21
+ ```bash
22
+ npm install react react-dom react-hook-form zod @hookform/resolvers \
23
+ @radix-ui/react-checkbox @radix-ui/react-select @radix-ui/react-slider \
24
+ @radix-ui/react-popover cmdk react-day-picker date-fns lucide-react \
25
+ clsx tailwind-merge
26
+ ```
27
+
28
+ > The library uses Tailwind CSS class names. Make sure your project has Tailwind configured.
29
+
30
+ ## Quick start
31
+
32
+ ```tsx
33
+ import {
34
+ ValidatedForm,
35
+ TextField,
36
+ NumberField,
37
+ CheckboxField,
38
+ } from '@liam-michel/validated-form';
39
+ import { z } from 'zod';
40
+
41
+ const schema = z.object({
42
+ name: z.string().min(1, 'Name is required'),
43
+ age: z.number().min(1, 'Age is required'),
44
+ agree: z.boolean().refine((v) => v, 'You must agree'),
45
+ });
46
+
47
+ function MyForm() {
48
+ return (
49
+ <ValidatedForm
50
+ schema={schema}
51
+ defaultValues={{ name: '', age: 0, agree: false }}
52
+ onSubmit={async (data) => {
53
+ // data is fully typed as { name: string; age: number; agree: boolean }
54
+ console.log(data);
55
+ }}
56
+ >
57
+ <TextField<typeof schema>
58
+ name="name"
59
+ label="Name"
60
+ placeholder="Enter your name"
61
+ />
62
+ <NumberField<typeof schema> name="age" label="Age" />
63
+ <CheckboxField<typeof schema> name="agree" label="I agree to the terms" />
64
+ </ValidatedForm>
65
+ );
66
+ }
67
+ ```
68
+
69
+ ## `createForm` — schema-bound factory
70
+
71
+ For a cleaner API without manual generics, use `createForm` to generate a full set of typed components from a single schema:
72
+
73
+ ```tsx
74
+ import { createForm } from '@liam-michel/validated-form';
75
+ import { z } from 'zod';
76
+
77
+ const schema = z.object({
78
+ email: z.string().email(),
79
+ bio: z.string(),
80
+ role: z.enum(['admin', 'user', 'viewer']),
81
+ });
82
+
83
+ const { Form, TextField, TextAreaField, SelectField } = createForm(schema);
84
+
85
+ function ProfileForm() {
86
+ return (
87
+ <Form
88
+ onSubmit={async (data) => console.log(data)}
89
+ defaultValues={{ email: '', bio: '', role: 'user' }}
90
+ >
91
+ <TextField name="email" label="Email" />
92
+ <TextAreaField
93
+ name="bio"
94
+ label="Bio"
95
+ placeholder="Tell us about yourself"
96
+ />
97
+ <SelectField
98
+ name="role"
99
+ label="Role"
100
+ options={[
101
+ { value: 'admin', label: 'Admin' },
102
+ { value: 'user', label: 'User' },
103
+ { value: 'viewer', label: 'Viewer' },
104
+ ]}
105
+ />
106
+ </Form>
107
+ );
108
+ }
109
+ ```
110
+
111
+ ## `useValidatedForm` — external form control
112
+
113
+ Use the `useValidatedForm` hook when you need access to the form instance outside of the `<ValidatedForm>` component (e.g. for programmatic control, multi-step forms, or watching values):
114
+
115
+ ```tsx
116
+ import {
117
+ ValidatedForm,
118
+ TextField,
119
+ useValidatedForm,
120
+ } from '@liam-michel/validated-form';
121
+ import { z } from 'zod';
122
+
123
+ const schema = z.object({
124
+ name: z.string().min(1),
125
+ });
126
+
127
+ function ControlledForm() {
128
+ const form = useValidatedForm({
129
+ schema,
130
+ defaultValues: { name: '' },
131
+ });
132
+
133
+ return (
134
+ <ValidatedForm
135
+ schema={schema}
136
+ form={form}
137
+ onSubmit={async (data) => console.log(data)}
138
+ >
139
+ <TextField<typeof schema> name="name" label="Name" />
140
+ </ValidatedForm>
141
+ );
142
+ }
143
+ ```
144
+
145
+ ## Field components
146
+
147
+ All fields share these common props:
148
+
149
+ | Prop | Type | Description |
150
+ | ------------- | --------------------- | --------------------------------------------------- |
151
+ | `name` | typed key from schema | The field name (type-safe based on your Zod schema) |
152
+ | `label` | `string` | Label text displayed above the field |
153
+ | `description` | `string?` | Helper text displayed below the field |
154
+ | `placeholder` | `string?` | Placeholder text |
155
+ | `disabled` | `boolean?` | Disable the field |
156
+ | `className` | `string?` | Additional CSS class for the wrapper |
157
+ | `showReset` | `boolean?` | Show a reset button to restore the default value |
158
+
159
+ ### Available fields
160
+
161
+ | Component | Schema type | Extra props |
162
+ | ------------------ | --------------------- | -------------------------------------------------------------- |
163
+ | `TextField` | `z.string()` | `type?: HTMLInputTypeAttribute` (e.g. `"password"`, `"email"`) |
164
+ | `TextAreaField` | `z.string()` | — |
165
+ | `NumberField` | `z.number()` | — |
166
+ | `CheckboxField` | `z.boolean()` | — |
167
+ | `SelectField` | `z.enum(...)` | `options: { value, label }[]` |
168
+ | `SliderField` | `z.number()` | `min?`, `max?`, `step?` |
169
+ | `DateField` | `z.date()` | — |
170
+ | `MultiSelectField` | `z.array(z.string())` | `options: { value, label }[]` |
171
+
172
+ ## `ValidatedForm` props
173
+
174
+ | Prop | Type | Description |
175
+ | --------------- | ---------------------------------- | -------------------------------------------------------- |
176
+ | `schema` | `ZodType` | Zod schema for validation |
177
+ | `onSubmit` | `(data: Output) => Promise<void>` | Submit handler (receives validated & typed data) |
178
+ | `defaultValues` | `DefaultValues?` | Initial form values |
179
+ | `form` | `UseFormReturn?` | External form instance from `useValidatedForm` |
180
+ | `submitLabel` | `string?` | Custom label for the submit button (default: `"Submit"`) |
181
+ | `hideSubmit` | `boolean?` | Hide the default submit button |
182
+ | `renderSubmit` | `(state) => ReactNode` | Custom submit button renderer |
183
+ | `renderError` | `(message) => ReactNode` | Custom root error renderer |
184
+ | `children` | `ReactNode \| (form) => ReactNode` | Fields, or a render function receiving the form instance |
185
+
186
+ ### Error handling
187
+
188
+ When `onSubmit` throws, errors are handled automatically:
189
+
190
+ - **Field-level errors**: Throw `{ data: { fieldErrors: { fieldName: "message" } } }` to set errors on specific fields.
191
+ - **Root-level errors**: Any other thrown error displays its `message` as a form-level error.
192
+
193
+ ```tsx
194
+ <ValidatedForm
195
+ schema={schema}
196
+ onSubmit={async (data) => {
197
+ const res = await api.createUser(data);
198
+ if (!res.ok) {
199
+ // Field-level errors
200
+ throw { data: { fieldErrors: { email: 'Email already taken' } } };
201
+ }
202
+ }}
203
+ >
204
+ {/* ... */}
205
+ </ValidatedForm>
206
+ ```
207
+
208
+ ## Type helpers
209
+
210
+ The library exports type helpers to extract field names by type from a schema:
211
+
212
+ ```tsx
213
+ import type {
214
+ StringFieldsOf,
215
+ NumberFieldsOf,
216
+ BooleanFieldsOf,
217
+ DateFieldsOf,
218
+ SelectFieldsOf,
219
+ } from '@liam-michel/validated-form';
220
+ ```
221
+
222
+ These are used internally by the field components and can be useful if you're building custom fields.
223
+
224
+ ## License
225
+
226
+ MIT