Skip to content

Commit 54652ee

Browse files
authored
feat: Add Yup and Zod validator support (#462)
* chore: first pass * chore: onto something I think * chore: closer but no cigar * chore: infer validator * chore: infer zod * feat: add validation transformer logic * chore: fix typings for react adapter * chore: fix issue with `this` not being defined properly * chore: mostly update FieldInfo types from Vue * chore: work on fixing type inferencing * fix: make ValidatorType optional * chore: make TName restriction easier to grok * chore: fix React types * chore: fix Vue types * chore: fix typing issues * chore: fix various linting items * chore: fix ESlint and validation logic * chore: fix inferencing from formdata * chore: fix form inferencing * chore: fix React TS types to match form validator logic * chore: fix Vue types * chore: migrate zod validation to dedicated package * chore: add first integration test for zod adapter * chore: enable non-validator types to be passed to validator * feat: add yup 1.x adapter * chore: add functionality and tests for form-wide validators * chore: fix typings of async validation types * fix: async validation should now run as-expected more often * chore: add async tests for Yup validator * chore: rename packages to match naming schema better * chore: add Zod examples for React and Vue * chore: add React and Vue Yup support * chore: fix formatting * chore: fix CI types * chore: initial work to drastically improve docs * docs: improve docs for validation * docs: add adapter validation docs
1 parent b35ecd1 commit 54652ee

File tree

104 files changed

+4158
-1779
lines changed

Some content is hidden

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

104 files changed

+4158
-1779
lines changed

docs/config.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
"label": "Guides",
3131
"children": [
3232
{
33-
"label": "Important Defaults",
34-
"to": "guides/important-defaults"
33+
"label": "Basic Concepts",
34+
"to": "guides/basic-concepts"
35+
},
36+
{
37+
"label": "Form Validation",
38+
"to": "guides/validation"
3539
}
3640
]
3741
},
@@ -89,6 +93,14 @@
8993
{
9094
"label": "Simple",
9195
"to": "framework/react/examples/simple"
96+
},
97+
{
98+
"label": "Yup",
99+
"to": "framework/react/examples/yup"
100+
},
101+
{
102+
"label": "Zod",
103+
"to": "framework/react/examples/zod"
92104
}
93105
]
94106
}
@@ -133,6 +145,14 @@
133145
{
134146
"label": "Simple",
135147
"to": "framework/vue/examples/simple"
148+
},
149+
{
150+
"label": "Yup",
151+
"to": "framework/vue/examples/yup"
152+
},
153+
{
154+
"label": "Zod",
155+
"to": "framework/vue/examples/zod"
136156
}
137157
]
138158
}

docs/guides/basic-concepts.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ title: Basic Concepts and Terminology
55

66
# Basic Concepts and Terminology
77

8-
> Some of these docs may be inaccurate due to an API shift in `0.11.0`. If you're interested in helping us fix these issues, please [join our Discord](https://tlinz.com/discord) and reach out in the `#form` channel.
9-
10-
This page introduces the basic concepts and terminology used in the @tanstack/react-form library. Familiarizing yourself with these concepts will help you better understand and work with the library.
8+
This page introduces the basic concepts and terminology used in the `@tanstack/react-form` library. Familiarizing yourself with these concepts will help you better understand and work with the library.
119

1210
## Form Factory
1311

@@ -86,7 +84,7 @@ Example:
8684

8785
## Validation
8886

89-
@tanstack/react-form provides both synchronous and asynchronous validation out of the box. Validation functions can be passed to the form.Field component using the validate and validateAsync props.
87+
`@tanstack/react-form` provides both synchronous and asynchronous validation out of the box. Validation functions can be passed to the form.Field component using the validate and validateAsync props.
9088

9189
Example:
9290

@@ -111,9 +109,34 @@ Example:
111109
/>
112110
```
113111

112+
## Validation Adapters
113+
114+
In addition to hand-rolled validation options, we also provide adapters like `@tanstack/zod-form-adapter` and `@tanstack/yup-form-adapter` to enable usage with common schema validation tools like [Yup](https://github.com/jquense/yup) and [Zod](https://zod.dev/).
115+
116+
Example:
117+
118+
```tsx
119+
<form.Field
120+
name="firstName"
121+
onChange={z
122+
.string()
123+
.min(3, "First name must be at least 3 characters")}
124+
onChangeAsyncDebounceMs={500}
125+
onChangeAsync={z.string().refine(
126+
async (value) => {
127+
await new Promise((resolve) => setTimeout(resolve, 1000));
128+
return !value.includes("error");
129+
},
130+
{
131+
message: "No 'error' allowed in first name",
132+
},
133+
)}
134+
/>
135+
```
136+
114137
## Reactivity
115138

116-
@tanstack/react-form offers various ways to subscribe to form and field state changes, such as the form.useStore hook, the form.Subscribe component, and the form.useField hook. These methods allow you to optimize your form's rendering performance by only updating components when necessary.
139+
`@tanstack/react-form` offers various ways to subscribe to form and field state changes, such as the `form.useStore` hook, the `form.Subscribe` component, and the `form.useField` hook. These methods allow you to optimize your form's rendering performance by only updating components when necessary.
117140

118141
Example:
119142

@@ -240,8 +263,5 @@ Example:
240263
/>
241264
```
242265

243-
These are the basic concepts and terminology used in the @tanstack/react-form library. Understanding these concepts will help you work more effectively with the library and create complex forms with ease.
266+
These are the basic concepts and terminology used in the `@tanstack/react-form` library. Understanding these concepts will help you work more effectively with the library and create complex forms with ease.
244267

245-
```
246-
247-
```

docs/guides/important-defaults.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)