Skip to content

Commit

Permalink
Add content to async validation guide on website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 13, 2024
1 parent a82c0a3 commit 0fd6f12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 40 additions & 1 deletion website/src/routes/guides/(advanced)/async-validation/index.mdx
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)]),
});
```
3 changes: 3 additions & 0 deletions website/src/routes/guides/(schemas)/other/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: Other
description: >-
This guide explains other special schema functions such as literal, instance,
special and recursive that are not covered in the other guides.
contributors:
- fabian-hiller
---
Expand Down

0 comments on commit 0fd6f12

Please sign in to comment.