zodify-json
v0.1.0
Published
Convert sample JSON responses into Zod schemas.
Maintainers
Readme
zodify
Convert sample JSON responses into Zod schemas.
Usage
# From a file
zodify response.json
# From stdin
cat response.json | zodify
# Non-interactive with all flags
zodify -n --object-mode=strict -m separate -a response.json -o schema.tsOptions
| Flag | Description |
|------|-------------|
| -n, --non-interactive | Skip all interactive prompts (requires config flags) |
| --object-mode=<strict\|loose> | strict rejects unknown keys; loose allows them |
| -m, --nested-mode=<nested\|separate> | nested inlines objects; separate exports each as a named schema |
| -p, --optional <path,path,...> | Comma-separated dotted paths to mark as optional |
| -a, --optional-all | Mark every field as optional |
| -o, --output <file> | Write output to a file instead of stdout |
| -h, --help | Show help |
Interactive Mode
When run without --non-interactive, zodify guides you through:
- Object mode — strict vs loose validation
- Nested schemas — inline definitions or separate named exports
- Optional fields — checkbox list of every field in the JSON
Examples
Basic JSON
$ echo '{"name": "Ada", "age": 36}' | zodifyimport { z } from "zod";
export const schema = z.strictObject({
name: z.string(),
age: z.number(),
});Arrays with merged objects
$ echo '[{"a": 1}, {"a": 2, "b": 3}]' | zodify -n --object-mode=loose -aimport { z } from "zod";
export const schema = z.array(
z.looseObject({
a: z.number().optional(),
b: z.number().optional(),
})
);Separate nested schemas
$ cat user.json | zodify -n --object-mode=strict -m separate -aimport { z } from "zod";
export const addressSchema = z.strictObject({
city: z.string().optional(),
zip: z.string().optional(),
});
export const schema = z.strictObject({
name: z.string().optional(),
address: addressSchema.optional(),
});How it works
- Infers types from JSON primitives (
string,number,boolean,null) - Merges object shapes across array items so missing keys become optional
- Handles nullable values (
nullmixed with other types) - Empty arrays fall back to
z.unknown() - Mixed-type arrays fall back to
z.unknown()
Development
npm install
npm run build
npm test