Skip to content

Commit 9761ecc

Browse files
feat: Merged Zod v3, v4, v4-mini support (#181)
1 parent c861ba9 commit 9761ecc

106 files changed

Lines changed: 892 additions & 435 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/docs/components/landing/interactive-demo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function InteractiveDemo() {
5656

5757
return (
5858
<div className="grid md:grid-cols-2 gap-1 w-full">
59-
<div className="bg-white rounded-lg md:py-6 md:px-0">
59+
<div className="bg-white rounded-lg p-1 md:py-6 md:px-1">
6060
<Editor
6161
className="md:h-[500px] md:border-0 h-[310px] border"
6262
options={editorOptions}

apps/docs/components/landing/sections/alert-banner.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ function AlertBanner() {
55
return (
66
<Alert
77
variant="destructive"
8-
className="border-amber-200 bg-amber-50/80 backdrop-blur-sm shadow-lg"
8+
className="border-amber-200 bg-amber-50/80 dark:border-amber-800/50 dark:bg-amber-950/30 backdrop-blur-sm shadow-lg dark:shadow-amber-900/20 transition-colors duration-200"
99
>
1010
<AlertDescription className="flex items-start space-x-3">
1111
<div className="text-xl">🚧</div>
12-
<div className="text-amber-800 leading-relaxed ">
13-
<span className="font-bold text-amber-900 text-base">
12+
<div className="text-amber-800 dark:text-amber-200 leading-relaxed">
13+
<span className="font-bold text-amber-900 dark:text-amber-100 text-base">
1414
Work in Progress{" "}
1515
</span>
1616
AutoForm as a standalone package is still work in progress. If you
1717
want to help out, please check out the{" "}
1818
<a
1919
href="https://github.com/vantezzen/auto-form"
20-
className="text-blue-600 hover:text-blue-800 decoration-blue-600 underline font-medium transition-colors"
20+
className="text-blue-600 hover:text-blue-800 decoration-blue-600 dark:text-blue-400 dark:hover:text-blue-300 underline font-medium transition-colors"
2121
target="_blank"
2222
rel="noopener noreferrer"
2323
>

apps/docs/content/docs/react/custom-integration.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Create a new file called `src/index.tsx` that will export all the necessary comp
3131
// src/index.tsx
3232
export * from "./AutoForm";
3333
export * from "./types";
34-
export * from "./utils";
3534
```
3635

3736
## Define custom types

apps/docs/content/docs/react/customization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: Customization
33
description: The customization of the components is done by providing a `fieldConfig` to your schema fields. This allows you to customize the rendering of the field, add additional props, and more.
44
---
55

6-
With zod, you can use the `superRefine` method to add a `fieldConfig` to your schema field. For more information, see the [Zod documentation](/docs/schema/zod).
6+
With zod, you can use the `superRefine` method to add a `fieldConfig` to your schema field. For more information, see the [Zod documentation](/docs/schema-providers/zod).
77

8-
With yup, you can use the `transform` method to add a `fieldConfig` to your schema field. For more information, see the [Yup documentation](/docs/schema/yup).
8+
With yup, you can use the `transform` method to add a `fieldConfig` to your schema field. For more information, see the [Yup documentation](/docs/schema-providers/yup).
99

10-
With joi, you can use the `meta` method to add a `fieldConfig` to your schema field. For more information, see the [Joi documentation](/docs/schema/joi).
10+
With joi, you can use the `meta` method to add a `fieldConfig` to your schema field. For more information, see the [Joi documentation](/docs/schema-providers/joi).
1111

1212
In these examples, we will use Zod.
1313

apps/docs/content/docs/react/getting-started.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ All children passed to the `AutoForm` component will be rendered below the form.
270270

271271
## Next Steps
272272

273-
- [How to define your zod schema](/docs/schema/zod)
274-
- [How to define your yup schema](/docs/schema/yup)
275-
- [How to define your joi schema](/docs/schema/joi)
273+
- [How to define your zod schema](/docs/schema-providers/zod)
274+
- [How to define your yup schema](/docs/schema-providers/yup)
275+
- [How to define your joi schema](/docs/schema-providers/joi)
276276
- [How to customize your form](/docs/react/customization)
277277
- [How to use AutoForm with other UI libraries](/docs/react/custom-integration)

apps/docs/content/docs/schema-providers/zod.md

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ Autoform is able to automatically transform some of zod's validation elements in
115115

116116
Validation methods that are not supported by HTML will automatically be checked when the form is submitted.
117117

118-
#### Descriptions
118+
#### Labels
119119

120-
You can use the `describe` method to set a label and description for each field. If no label is set, the field name will be used and un-camel-cased.
120+
You can use the `describe` method to set a label for each field. If no label is set, the field name will be used and un-camel-cased.
121121

122122
```tsx
123123
const formSchema = z.object({
@@ -246,14 +246,19 @@ const formSchema = z.object({
246246
#### Field configuration
247247

248248
You can use the `fieldConfig` function to set additional configuration for how a field should be rendered. This function is independent of the UI library you use so you can provide the FieldTypes that are supported by your UI library.
249+
It's recommended to create your own `fieldConfig` function. Use the base `buildZodFieldConfig` from `@autoform/react` and extend it with your customizations to ensure full TypeScript support.
249250

250-
**Zod v3:**
251+
> `ZodProvider` and `fieldConfig` from `@autoform/zod` are fully compatible with all Zod versions including v3, v4, and Zod Mini, allowing you to use them across any Zod version.
251252
252-
For Zod v3, it's recommended that you create your own fieldConfig function that uses the base `buildZodFieldConfig` function from `@autoform/react` and adds your own customizations. You apply this configuration using the `.superRefine()` method.
253+
#### Zod v3
254+
255+
With Zod v3, you apply this configuration using the `.superRefine()` method.
253256

254257
```tsx
258+
import { ZodProvider, fieldConfig } from "@autoform/zod";
255259
import { buildZodFieldConfig } from "@autoform/react";
256260
import { FieldTypes } from "@autoform/mui"; // or your UI library's FieldTypes
261+
import * as z from "zod";
257262

258263
const fieldConfig = buildZodFieldConfig<
259264
FieldTypes,
@@ -264,6 +269,15 @@ const fieldConfig = buildZodFieldConfig<
264269

265270
// Example usage with Zod v3
266271
const formSchemaV3 = z.object({
272+
username: z
273+
.string({
274+
required_error: "Username is required.",
275+
})
276+
.superRefine(
277+
fieldConfig({
278+
description: "You cannot change this later.",
279+
})
280+
),
267281
password: z.string().superRefine(
268282
fieldConfig<React.ReactNode, FieldTypes>({
269283
description: "We recommend to use a strong password.",
@@ -278,16 +292,15 @@ const formSchemaV3 = z.object({
278292
});
279293
```
280294

281-
**Zod v4:**
282-
283-
With Zod v4, the method for applying `fieldConfig` has changed. You now use the `fieldConfig` function directly from `@autoform/zod/v4` and apply it using the `.register()` method provided by Zod v4.
295+
#### Zod v4
284296

297+
With Zod v4, you apply this configuration using the `.check()` method.
285298
Here's an example demonstrating the new usage:
286299

287300
```tsx
301+
import { ZodProvider, fieldConfig } from "@autoform/zod";
302+
import { AutoForm, FieldTypes } from "@autoform/mui";
288303
import * as z from "zod/v4";
289-
import { ZodProvider, fieldConfig } from "@autoform/zod/v4"; // Import fieldConfig from v4
290-
import { AutoForm, FieldTypes } from "@autoform/mui"; // or your UI library's FieldTypes
291304

292305
// Define your form schema using zod v4
293306
const formSchemaV4 = z.object({
@@ -302,8 +315,8 @@ const formSchemaV4 = z.object({
302315
message: "Password must be at least 8 characters.",
303316
})
304317
// Use .register() with the spread operator (...)
305-
.register(
306-
...fieldConfig<React.ReactNode, FieldTypes>({
318+
.check(
319+
fieldConfig<React.ReactNode, FieldTypes>({
307320
description: "We recommend to use a strong password.",
308321
inputProps: {
309322
type: "password",
@@ -318,12 +331,12 @@ const formSchemaV4 = z.object({
318331
// ... other fields
319332
});
320333

321-
const schemaProviderV4 = new ZodProvider(formSchemaV4);
334+
const schemaProvider = new ZodProvider(formSchemaV4);
322335

323336
function AppV4() {
324337
return (
325338
<AutoForm
326-
schema={schemaProviderV4}
339+
schema={schemaProvider}
327340
onSubmit={(data) => {
328341
console.log(data);
329342
}}
@@ -333,16 +346,48 @@ function AppV4() {
333346
}
334347
```
335348

336-
Notice the use of the spread operator (`...`) before `fieldConfig` when calling `.register()`. This is necessary because `register` expects two arguments, and the `fieldConfig` function from `@autoform/zod/v4` returns a tuple `[key, value]`. Also note that you should import `ZodProvider` and `fieldConfig` from `@autoform/zod/v4` when using Zod v4.
349+
#### Zod Mini
350+
351+
With Zod mini, you apply this configuration using the `.check()` method.
337352

338353
```tsx
339-
import { buildZodFieldConfig } from "@autoform/react";
340-
import { FieldTypes } from "@autoform/mui";
354+
import { ZodProvider, fieldConfig } from "@autoform/zod";
355+
import { AutoForm, FieldTypes } from "@autoform/mui";
356+
import * as z from "zod/v4-mini";
341357

342-
const fieldConfig = buildZodFieldConfig<
343-
FieldTypes,
344-
{
345-
isImportant?: boolean;
346-
}
347-
>();
358+
// Define your form schema using zod mini
359+
const formSchemaMini = z.object({
360+
password: zm
361+
.string({
362+
error: "Password is required.",
363+
})
364+
.check(
365+
zm.minLength(8, {
366+
message: "Password must be at least 8 characters.",
367+
})
368+
)
369+
.check(
370+
fieldConfig({
371+
// Changed from superRefine to register
372+
description: "Always use a secure password!",
373+
inputProps: {
374+
type: "password",
375+
},
376+
})
377+
),
378+
379+
sendMeMails: zm.optional(zm.boolean()).check(
380+
fieldConfig({
381+
// Changed from superRefine to register
382+
fieldWrapper: (props: FieldWrapperProps) => (
383+
<>
384+
{props.children}
385+
<p className="text-muted-foreground text-xs">
386+
Don't worry, we only send important emails!
387+
</p>
388+
</>
389+
),
390+
})
391+
),
392+
});
348393
```

apps/web/components/Ant.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use client";
22
import { AutoForm } from "@autoform/ant";
3-
import { joiSchemaProvider } from "./utils";
3+
import { zodSchemaProvider } from "./utils";
44

55
const Ant = () => {
66
return (
77
<AutoForm
8-
schema={joiSchemaProvider}
8+
schema={zodSchemaProvider}
99
onSubmit={(data) => {
1010
console.log(JSON.stringify(data, null, 2));
1111
}}

apps/web/components/Chakra.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutoForm, fieldConfig } from "@autoform/chakra";
1+
import { AutoForm } from "@autoform/chakra";
22
import { zodSchemaProvider } from "./utils";
33

44
function Chakra() {

apps/web/components/Mantine.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createTheme, MantineProvider } from "@mantine/core";
2-
import { AutoForm, fieldConfig } from "@autoform/mantine";
2+
import { AutoForm } from "@autoform/mantine";
33
import { zodSchemaProvider } from "./utils";
44
import "@mantine/core/styles.css";
55

0 commit comments

Comments
 (0)