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
Author
10 person written this
allieschen
open
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;
}
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?
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,
});
demo Article's
30 articles in total
Using Disposable Emails for a Demo
read article
Profylix
read article
Demo: Automating GitHub Repo Configuration and Security with Minder
read article
How to do great live demos β and why theyβre important to get right
read article
Flems.io
read article
Three Tips for Your Next (Software) Demo
read article
Using Valibot for Recursive Schema Validation
currently reading
Demo: Minder, a software supply chain security platform from Stacklok
read article
asdsadasd
read article
Building an Anime Recommendation System with PySpark in SageMaker
read article
Demo Highlight: Acey-Deucey
read article
Demo Highlight: Asteroids
read article
Writting Simple OnceCell
read article
How to build a demo project
read article
Playing around with Ultra HDR
read article
Odoo CRM Demo
read article
Odoo Demo
read article
demo
read article
Demo Highlight: 2dVis
read article
Pimcore Demo installed locally but can not log into Admin using the credentials set at the time of installation
read article
Learn how to access webcam and take photo with JavaScript
read article
Futuristic Dial Button β
read article
Python SEO Keyword Research Tool: Google Autocomplete, People Also Ask and Related Searches
read article
Enterprise Power BI Series
read article
Dropshipping Research Tool Demo in Python
read article
How to run Apache SkyWalking on AWS EKS and RDS/Aurora
read article
New js framework
read article
Top 10 trending github repos for Java developers in this weekπΈ.
read article
Floating Islands WebGL demo πΊπ¦
read article
ReactJS Demo Project - Party Planner Web App - Github
read article
Featured ones: