Generate a Zod schema for validating JSON inventory
Admin ·
Generate a runtime validator from records that show the values your frontend receives. Check one valid record and one deliberate error to see what the inferred Zod schema accepts before using it at an API boundary.
Test the generated schema against inventory records
Choose Load example and Generate Zod schema. The output exports inventorySchema and an Inventory type. Labels is an array whose elements accept a string or safe integer, and updatedAt receives an ISO datetime check.
Run inventorySchema.safeParse on the example in a project with Zod 4. Then change a label to true and repeat. It should fail the item rule. Keep the example contact omitted on the first record. It is optional because the sample omits it.
Check when a note may be null or missing
The note is nullable by default. It is present in both records, so removing it altogether should fail validation. Choose Accept null or missing, generate again and retry after omitting the note.
Choose Reject keys under Unknown object keys and add a new field to the first warehouse. The generated strict object schema rejects that key. Choose Keep keys if the consumer needs to retain new fields.
Check large IDs before trusting the sampled number type
The first item-id in the example exceeds JavaScript's safe integer range. Read the Review notes warning. The generator emits z.number() for the ID, and standard JSON.parse may already have changed its least significant digit before Zod validates it.
To preserve every digit, confirm that the API can return the ID as a quoted string and regenerate from string IDs. Include the inferred TypeScript type if consumers need compile-time hints alongside the validator.
Zod 4 schema definitions and object optionsECMAScript Number.isSafeInteger definition
Common questions
Can I generate a selected nested schema only?
Yes. Use Root JSON Pointer to choose a nested value, such as /0/warehouse in the inventory example.
Does a string format become a runtime check?
With Infer string format validators on, consistently recognized sample email addresses, HTTP or HTTPS URLs, UUIDs and ISO datetimes generate corresponding Zod 4 validators.