|
| 1 | +import * as v from "valibot"; |
| 2 | + |
| 3 | +// User Schema |
| 4 | +export const userSchema = v.object({ |
| 5 | + name: v.string(), |
| 6 | + age: v.optional(v.number(), 42), |
| 7 | + phone: v.pipe( |
| 8 | + v.optional(v.union([v.string(), v.number()]), "123-456-7890"), |
| 9 | + v.transform((phone: string | number) => |
| 10 | + typeof phone === "number" ? phone.toString() : phone |
| 11 | + ) |
| 12 | + ), |
| 13 | +}); |
| 14 | + |
| 15 | +export type User = v.InferOutput<typeof userSchema>; |
| 16 | +export type UserInput = v.InferInput<typeof userSchema>; |
| 17 | + |
| 18 | +// 1. Product Schema |
| 19 | +export const productSchema = v.object({ |
| 20 | + id: v.string(), |
| 21 | + name: v.string(), |
| 22 | + price: v.pipe(v.number(), v.minValue(0.01)), |
| 23 | + inStock: v.boolean(), |
| 24 | + tags: v.array(v.string()), |
| 25 | +}); |
| 26 | +export type Product = v.InferOutput<typeof productSchema>; |
| 27 | + |
| 28 | +// 2. Order Schema |
| 29 | +export const orderSchema = v.object({ |
| 30 | + orderId: v.pipe(v.string(), v.uuid()), |
| 31 | + userId: v.string(), |
| 32 | + items: v.array(productSchema), |
| 33 | + totalAmount: v.number(), |
| 34 | + orderDate: v.date(), |
| 35 | + status: v.picklist(["pending", "shipped", "delivered", "cancelled"]), |
| 36 | +}); |
| 37 | +export type Order = v.InferOutput<typeof orderSchema>; |
| 38 | + |
| 39 | +// 3. Address Schema |
| 40 | +export const addressSchema = v.object({ |
| 41 | + street: v.string(), |
| 42 | + city: v.string(), |
| 43 | + state: v.string(), |
| 44 | + zipCode: v.pipe(v.string(), v.regex(/^[0-9]{5}(?:-[0-9]{4})?$/)), |
| 45 | + country: v.optional(v.string(), "USA"), |
| 46 | +}); |
| 47 | +export type Address = v.InferOutput<typeof addressSchema>; |
| 48 | + |
| 49 | +// 4. Customer Schema |
| 50 | +export const customerSchema = v.object({ |
| 51 | + customerId: v.string(), |
| 52 | + basicInfo: userSchema, |
| 53 | + shippingAddress: addressSchema, |
| 54 | + billingAddress: v.nullable(addressSchema), |
| 55 | + purchaseHistory: v.optional(v.array(orderSchema)), |
| 56 | +}); |
| 57 | +export type Customer = v.InferOutput<typeof customerSchema>; |
| 58 | +export type CustomerInput = v.InferInput<typeof customerSchema>; |
| 59 | + |
| 60 | +// 5. BlogPost Schema |
| 61 | +export const blogPostSchema = v.object({ |
| 62 | + postId: v.pipe(v.number(), v.minValue(1)), |
| 63 | + title: v.string(), |
| 64 | + content: v.string(), |
| 65 | + authorId: v.string(), |
| 66 | + publishDate: v.optional(v.date()), |
| 67 | + metadata: v.object({ |
| 68 | + views: v.pipe(v.number(), v.integer(), v.minValue(0)), |
| 69 | + likes: v.pipe(v.number(), v.integer(), v.minValue(0)), |
| 70 | + category: v.string(), |
| 71 | + }), |
| 72 | +}); |
| 73 | +export type BlogPost = v.InferOutput<typeof blogPostSchema>; |
| 74 | + |
| 75 | +// 6. Configuration Schema |
| 76 | +export const configSchema = v.object({ |
| 77 | + apiKey: v.string(), |
| 78 | + timeoutMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1)), 5000), |
| 79 | + retries: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(5)), |
| 80 | + featureFlags: v.object({ |
| 81 | + betaFeature: v.optional(v.boolean()), |
| 82 | + newUI: v.optional(v.boolean(), false), |
| 83 | + }), |
| 84 | + logLevel: v.optional(v.picklist(["debug", "info", "warn", "error"])), |
| 85 | +}); |
| 86 | +export type Config = v.InferOutput<typeof configSchema>; |
| 87 | +export type ConfigInput = v.InferInput<typeof configSchema>; |
| 88 | + |
| 89 | +// 7. Event Schema |
| 90 | +export const eventSchema = v.object({ |
| 91 | + eventId: v.string(), |
| 92 | + eventName: v.string(), |
| 93 | + timestamp: v.number(), |
| 94 | + payload: v.unknown(), |
| 95 | + source: v.picklist(["web", "mobile", "server"]), |
| 96 | +}); |
| 97 | +export type Event = v.InferOutput<typeof eventSchema>; |
| 98 | + |
| 99 | +// 8. Image Metadata Schema |
| 100 | +export const imageMetadataSchema = v.object({ |
| 101 | + url: v.pipe(v.string(), v.url()), |
| 102 | + width: v.pipe(v.number(), v.integer(), v.minValue(1)), |
| 103 | + height: v.pipe(v.number(), v.integer(), v.minValue(1)), |
| 104 | + format: v.picklist(["jpeg", "png", "gif", "webp"]), |
| 105 | + caption: v.optional(v.pipe(v.string(), v.maxLength(255))), |
| 106 | +}); |
| 107 | +export type ImageMetadata = v.InferOutput<typeof imageMetadataSchema>; |
| 108 | + |
| 109 | +// 9. Employee Schema |
| 110 | +export const employeeSchema = v.object({ |
| 111 | + employeeId: v.string(), |
| 112 | + firstName: v.string(), |
| 113 | + lastName: v.string(), |
| 114 | + email: v.pipe(v.string(), v.email()), |
| 115 | + department: v.string(), |
| 116 | + startDate: v.date(), |
| 117 | + isActive: v.boolean(), |
| 118 | + managerId: v.optional(v.nullable(v.string())), |
| 119 | +}); |
| 120 | +export type Employee = v.InferOutput<typeof employeeSchema>; |
| 121 | + |
| 122 | +// 10. Task Schema |
| 123 | +export const taskSchema = v.object({ |
| 124 | + taskId: v.string(), |
| 125 | + description: v.string(), |
| 126 | + dueDate: v.optional(v.date()), |
| 127 | + priority: v.picklist(["low", "medium", "high"]), |
| 128 | + completed: v.optional(v.boolean(), false), |
| 129 | + assignee: v.nullable(employeeSchema), |
| 130 | +}); |
| 131 | +export type Task = v.InferOutput<typeof taskSchema>; |
| 132 | +export type TaskInput = v.InferInput<typeof taskSchema>; |
0 commit comments