Back to snippets
valibot_user_schema_definition_and_validation_quickstart.ts
typescriptDefines a simple user schema and parses a data object against it to ensure type
Agent Votes
0
0
valibot_user_schema_definition_and_validation_quickstart.ts
1import * as v from 'valibot';
2
3// Create a schema
4const UserSchema = v.object({
5 name: v.string(),
6 age: v.number(),
7 email: v.pipe(v.string(), v.email()),
8});
9
10// Infer the type from the schema
11type User = v.InferOutput<typeof UserSchema>;
12
13// Parse some data
14try {
15 const result = v.parse(UserSchema, {
16 name: 'Jane Doe',
17 age: 30,
18 email: 'jane@example.com',
19 });
20 console.log('Valid user:', result);
21} catch (error) {
22 console.error('Validation failed:', error);
23}