|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Schema Serialization Demo |
| 5 | + * |
| 6 | + * This demo shows how to serialize and deserialize schema types to/from JSON. |
| 7 | + * This is useful for: |
| 8 | + * - Sending schema definitions over the network |
| 9 | + * - Storing schemas in databases |
| 10 | + * - Sharing schemas between different systems |
| 11 | + */ |
| 12 | + |
| 13 | +import { array, deserializeSchema, int, object, serializeSchema, string, z } from 'zeed' |
| 14 | + |
| 15 | +// Define a user schema |
| 16 | +const userSchema = z.object({ |
| 17 | + id: z.string().default('auto-generated'), |
| 18 | + name: z.string().describe('Full name of the user'), |
| 19 | + email: z.string(), |
| 20 | + age: z.int().optional(), |
| 21 | + role: z.enum(['admin', 'user', 'guest']).default('user'), |
| 22 | + tags: z.array(z.string()).optional(), |
| 23 | + metadata: z.object({ |
| 24 | + createdAt: z.string(), |
| 25 | + updatedAt: z.string().optional(), |
| 26 | + }).optional(), |
| 27 | +}) |
| 28 | + |
| 29 | +console.log('Original schema:') |
| 30 | +console.log(userSchema) |
| 31 | + |
| 32 | +// Serialize the schema to a plain JSON object |
| 33 | +const serialized = serializeSchema(userSchema) |
| 34 | +console.log('\nSerialized schema:') |
| 35 | +console.log(JSON.stringify(serialized, null, 2)) |
| 36 | + |
| 37 | +// Convert to JSON string (as you might send over network) |
| 38 | +const jsonString = JSON.stringify(serialized) |
| 39 | +console.log('\nJSON string length:', jsonString.length) |
| 40 | + |
| 41 | +// Deserialize back to a schema |
| 42 | +const deserialized = deserializeSchema(JSON.parse(jsonString)) |
| 43 | +console.log('\nDeserialized schema:') |
| 44 | +console.log(deserialized) |
| 45 | + |
| 46 | +// Use the deserialized schema to validate data |
| 47 | +const userData = { |
| 48 | + name: 'Alice Smith', |
| 49 | + |
| 50 | + age: 30, |
| 51 | + role: 'admin', |
| 52 | + tags: ['developer', 'typescript'], |
| 53 | +} |
| 54 | + |
| 55 | +console.log('\nValidating data with deserialized schema:') |
| 56 | +console.log('Input:', userData) |
| 57 | + |
| 58 | +// Note: You would use a parsing function here when available |
| 59 | +// For now, we can just verify the schema structure is correct |
| 60 | +console.log('Schema type:', deserialized.type) |
| 61 | +console.log('Schema has object definition:', !!deserialized._object) |
| 62 | +console.log('Name field type:', deserialized._object.name.type) |
| 63 | +console.log('Role field enum values:', deserialized._object.role._enumValues) |
| 64 | +console.log('Age field is optional:', deserialized._object.age._optional) |
| 65 | + |
| 66 | +// Demonstrate round-trip serialization |
| 67 | +const reserialized = serializeSchema(deserialized) |
| 68 | +const areEqual = JSON.stringify(serialized) === JSON.stringify(reserialized) |
| 69 | +console.log('\nRound-trip successful:', areEqual) |
| 70 | + |
| 71 | +// Example of a complex nested schema |
| 72 | +const apiResponseSchema = object({ |
| 73 | + success: z.boolean(), |
| 74 | + data: z.object({ |
| 75 | + users: array(userSchema), |
| 76 | + total: int(), |
| 77 | + }), |
| 78 | + error: string().optional(), |
| 79 | +}) |
| 80 | + |
| 81 | +console.log('\n--- Complex Schema Example ---') |
| 82 | +const complexSerialized = serializeSchema(apiResponseSchema) |
| 83 | +console.log('Complex schema serialized:') |
| 84 | +console.log(JSON.stringify(complexSerialized, null, 2)) |
| 85 | + |
| 86 | +const complexDeserialized = deserializeSchema(complexSerialized) |
| 87 | +console.log('\nComplex schema deserialized successfully:', complexDeserialized.type === 'object') |
0 commit comments