Skip to content

Commit

Permalink
docs: Improve text and Valibot example (#518)
Browse files Browse the repository at this point in the history
* docs: improve validation adapter text

* fix: simplify async Valibot string validation
  • Loading branch information
fabian-hiller authored Nov 10, 2023
1 parent 0c88ee8 commit 1925b67
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 50 deletions.
2 changes: 1 addition & 1 deletion docs/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ This will debounce every async call with a 500ms delay. You can even override th

While functions provide more flexibility and customization over your validation, they can be a bit verbose. To help solve this, there are libraries like [Valibot](https://valibot.dev/), [Yup](https://github.com/jquense/yup), and [Zod](https://zod.dev/) that provide schema-based validation to make shorthand and type-strict validation substantially easier.

Luckily, we support both of these libraries through official adapters:
Luckily, we support all of these libraries through official adapters:

```bash
$ npm install @tanstack/zod-form-adapter zod
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ yarn add @tanstack/solid-form

> Depending on your environment, you might need to add polyfills. If you want to support older browsers, you need to transpile the library from `node_modules` yourselves.
In addition, we support both Zod and Yup as validators through official validator packages:
In addition, we support Zod, Yup, and Valibot as validators through official validator packages:

```bash
$ npm i @tanstack/zod-form-adapter zod
Expand Down
20 changes: 4 additions & 16 deletions examples/react/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import { createRoot } from "react-dom/client";
import { useForm } from "@tanstack/react-form";
import { valibotValidator } from "@tanstack/valibot-form-adapter";
import { string, minLength, stringAsync, PipeResult } from "valibot";
import { customAsync, minLength, string, stringAsync } from "valibot";
import type { FieldApi } from "@tanstack/react-form";

function FieldInfo({ field }: { field: FieldApi<any, any, unknown, unknown> }) {
Expand Down Expand Up @@ -50,22 +50,10 @@ export default function App() {
])}
onChangeAsyncDebounceMs={500}
onChangeAsync={stringAsync([
async (value) => {
customAsync(async (value) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return (
value.includes("error")
? {
issues: [
{
input: value,
validation: "firstName",
message: "No 'error' allowed in first name",
},
],
}
: { output: value }
) as PipeResult<string>;
},
return value.includes("error");
}, "No 'error' allowed in first name"),
])}
children={(field) => {
// Avoid hasty abstractions. Render props are great!
Expand Down
20 changes: 4 additions & 16 deletions examples/solid/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render } from 'solid-js/web'

import { createForm, type FieldApi } from '@tanstack/solid-form'
import { valibotValidator } from '@tanstack/valibot-form-adapter'
import { string, minLength, stringAsync, PipeResult } from 'valibot'
import { customAsync, minLength, string, stringAsync } from 'valibot'

interface FieldInfoProps {
field: FieldApi<any, any, unknown, unknown>
Expand Down Expand Up @@ -54,22 +54,10 @@ function App() {
])}
onChangeAsyncDebounceMs={500}
onChangeAsync={stringAsync([
async (value) => {
customAsync(async (value) => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return (
value.includes('error')
? {
issues: [
{
input: value,
validation: 'firstName',
message: "No 'error' allowed in first name",
},
],
}
: { output: value }
) as PipeResult<string>
},
return value.includes('error')
}, "No 'error' allowed in first name"),
])}
children={(field) => {
// Avoid hasty abstractions. Render props are great!
Expand Down
20 changes: 4 additions & 16 deletions examples/vue/valibot/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useForm } from '@tanstack/vue-form'
import FieldInfo from './FieldInfo.vue'
import { valibotValidator } from '@tanstack/valibot-form-adapter'
import { string, minLength, stringAsync, PipeResult } from 'valibot'
import { customAsync, minLength, string, stringAsync } from 'valibot'
const form = useForm({
defaultValues: {
Expand All @@ -20,22 +20,10 @@ const form = useForm({
form.provideFormContext()
const onChangeFirstName = stringAsync([
async (value) => {
customAsync(async (value) => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return (
value.includes('error')
? {
issues: [
{
input: value,
validation: 'firstName',
message: "No 'error' allowed in first name",
},
],
}
: { output: value }
) as PipeResult<string>
},
return value.includes('error')
}, "No 'error' allowed in first name"),
])
</script>

Expand Down

0 comments on commit 1925b67

Please sign in to comment.