-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add content to async validation guide on website
- Loading branch information
1 parent
a82c0a3
commit 0fd6f12
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 40 additions & 1 deletion
41
website/src/routes/guides/(advanced)/async-validation/index.mdx
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,9 +1,48 @@ | ||
--- | ||
title: Async validation | ||
description: >- | ||
By default, I validate each schema synchronously. This is usually the fastest | ||
way to validate unknown data, but sometimes you need to validate something | ||
asynchronously. | ||
contributors: | ||
- fabian-hiller | ||
--- | ||
|
||
import { Link } from '@builder.io/qwik-city'; | ||
|
||
# Async validation | ||
|
||
> The content of this page is not yet ready. Check back in a few days. | ||
By default, I validate each schema synchronously. This is usually the fastest way to validate unknown data, but sometimes you need to validate something asynchronously. For example, you might want to check if a username already exists in your database. | ||
|
||
## How it works | ||
|
||
To be able to do this, I provide an asynchronous implementation for almost every synchronous implementation. The only difference is that the asynchronous implementation always returns a promise. Otherwise, the API is exactly the same. | ||
|
||
### Naming | ||
|
||
The asynchronous implementation starts with the same name as the synchronous one, but adds the suffix `Async` to the end. For example, the asynchronous implementation of <Link href="/api/string/">`string`</Link> is called <Link href="/api/stringAsync/">`stringAsync`</Link> and the asynchronous implementation of <Link href="/api/object/">`object`</Link> is called <Link href="/api/objectAsync/">`objectAsync`</Link>. | ||
|
||
### Nesting | ||
|
||
Asynchronous functions can only be nested inside other asynchronous functions. This means that if you need to validate a string within an object asynchronously, you must also switch the object validation to the asynchronous implementation. | ||
|
||
This is not necessary in the other direction. You can nest synchronous functions within asynchronous functions, and we recommend that you do so in most cases to keep complexity and bundle size to a minimum. | ||
|
||
#### Rule of thumb | ||
|
||
We recommend that you always start with the synchronous implementation, and only move the necessary parts to the asynchronous implementation as needed. If you are using TypeScript, it is not possible to make a mistake here, as our API is completely type-safe and will notify you when you embed an asynchronous function into a synchronous function. | ||
|
||
### Example | ||
|
||
Let's say you want to validate a profile object and the username should be checked asynchronously against your database. Only the object and username validation needs to be asynchronous, the rest can stay synchronous. | ||
|
||
```ts | ||
import * as v from 'valibot'; | ||
import { isUsernameAvailable } from '~/api'; | ||
|
||
const ProfileSchema = v.objectAsync({ | ||
username: v.stringAsync([v.customAsync(isUsernameAvailable)]), | ||
avatar: v.string([v.url()]), | ||
description: v.string([v.maxLength(1000)]), | ||
}); | ||
``` |
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