Logo

dev-resources.site

for different kinds of informations.

Using Valibot for Recursive Schema Validation

Published at
5/12/2024
Categories
typescript
demo
valibot
Author
allieschen
Categories
3 categories in total
typescript
open
demo
open
valibot
open
Author
10 person written this
allieschen
open
Using Valibot for Recursive Schema Validation

Valibot's playground for the code demo

Recursive Type in TypeScript

TypeScript allows defining recursive types. For example, consider the following User interface:

interface User {
 children?: User;
 email: `${string}@${string}`;
 password: string;
}
Enter fullscreen mode Exit fullscreen mode

Using Valibot for Schema Validation

What if you use a schema library, like Zod or Valibot? The schema has been built on value-level, and you cannot assign the variant to its property inside the declaration.

import * as v from 'valibot';

const EmailSchema = v.string([v.minLength(1), v.email()]);
const PasswordSchema = v.string([v.minLength(1), v.minLength(8)]);
// const UserSchema?
Enter fullscreen mode Exit fullscreen mode

Recursive Schema

Base on the Author's reply in this issue, you can use v.lazy(() => UserSchema) and to create a type of the UserSchema as a type param to the v.BaseSchema genre as a type inference of the UserSchema:

type UserSchemaType = {
 children?: UserSchemaType;
 email: v.Input<typeof EmailSchema>;
 password: v.Input<typeof PasswordSchema>;
}

const UserSchema: v.BaseSchema<UserSchemaType> = v.object({
 children: v.optional(v.lazy(() => UserSchema)),
 email: EmailSchema,
 password: PasswordSchema,
});
Enter fullscreen mode Exit fullscreen mode
demo Article's
30 articles in total
Favicon
Using Disposable Emails for a Demo
Favicon
Profylix
Favicon
Demo: Automating GitHub Repo Configuration and Security with Minder
Favicon
How to do great live demos β€” and why they’re important to get right
Favicon
Flems.io
Favicon
Three Tips for Your Next (Software) Demo
Favicon
Using Valibot for Recursive Schema Validation
Favicon
Demo: Minder, a software supply chain security platform from Stacklok
Favicon
asdsadasd
Favicon
Building an Anime Recommendation System with PySpark in SageMaker
Favicon
Demo Highlight: Acey-Deucey
Favicon
Demo Highlight: Asteroids
Favicon
Writting Simple OnceCell
Favicon
How to build a demo project
Favicon
Playing around with Ultra HDR
Favicon
Odoo CRM Demo
Favicon
Odoo Demo
Favicon
demo
Favicon
Demo Highlight: 2dVis
Favicon
Pimcore Demo installed locally but can not log into Admin using the credentials set at the time of installation
Favicon
Learn how to access webcam and take photo with JavaScript
Favicon
Futuristic Dial Button ☎
Favicon
Python SEO Keyword Research Tool: Google Autocomplete, People Also Ask and Related Searches
Favicon
Enterprise Power BI Series
Favicon
Dropshipping Research Tool Demo in Python
Favicon
How to run Apache SkyWalking on AWS EKS and RDS/Aurora
Favicon
New js framework
Favicon
Top 10 trending github repos for Java developers in this week🍸.
Favicon
Floating Islands WebGL demo πŸ‡ΊπŸ‡¦
Favicon
ReactJS Demo Project - Party Planner Web App - Github

Featured ones: