Generate validated inquirer.js prompts from Zod schemas.
npm install zod-inquirerA TypeScript library that automatically generates validated CLI prompts from Zod schemas.
``bash`
npm install zod-inquirer
Define your schema once, get validated prompts automatically (example uses choices passed to zodPrompter):
`typescript
import { z } from "zod";
import { zodPrompter } from "zod-inquirer";
const PizzaSchema = z.object({
name: z.string().describe("Enter your name"), // Zod 3 style
email: z.email().meta({ description: "Enter your email" }), // Zod 4 style
age: z.number().int().positive(), // will use default description
size: z
.enum(["small", "medium", "large"])
.meta({ description: "Select your size" }),
crust: z
.enum(["thin", "thick", "stuffed"])
.meta({ description: "Select your crust" }),
deliveryWindow: z.string().meta({ description: "Select delivery window" }),
toppings: z
.array(z.string())
.min(1, { message: "You must select at least one topping." })
.max(3, { message: "You can select up to 3 toppings." })
.meta({ description: "Select up to 3 toppings" }),
acceptTerms: z.boolean().meta({ description: "Accept terms and conditions" }),
password: z.string().min(8).meta({ description: "Enter your password" }),
});
const pizza = await zodPrompter(PizzaSchema, {
choices: {
deliveryWindow: ["ASAP", "In 30 minutes", "In 1 hour", "Sometime later"],
toppings: ["pepperoni", "mushrooms", "onions", "sausage", "bacon", "extra cheese"],
},
});
console.log(pizza);
`
This will automatically prompt the user with:
- A text input for name
- A text input for age
- A number input for size
- A select dropdown for crust
- A select dropdown for deliveryWindow
- A select dropdown for toppings
- A checkbox list for acceptTerms
- A confirm checkbox for password
- A password input for
All inputs are validated against your Zod schema with automatic retry on validation failure.
`typescript`
await zodPrompter(schema, {
maxRetries: 3, // Maximum retry attempts for invalid input (default: 3)
hint: "press Ctrl+C to abort", // Hint message shown on validation error
// Provide static choices for specific fields (strings or { name, value } objects)
choices: {
deliveryWindow: ["ASAP", "In 30 minutes", "In 1 hour", "Sometime later"],
toppings: ["pepperoni", "mushrooms", "onions"],
},
});
Notes:
- The choices object is a mapping from schema field name to an array of choices.z.enum
- If choices are provided for a field, the prompter will use them instead of deriving options from .z.array(z.string())
- For array fields (e.g. ) the prompter will use a checkbox multi-select; for single-value fields (e.g. z.string()) it will use a select.{ name, value }
- Choices can be simple strings or objects with to control display text vs stored value.choices
- Ensure the keys in exactly match your schema field names (case-sensitive).
- z.string() - Text input, masked based on name: password, secret, token, apikey, apiKey, api_keyz.enum()
- - Select dropdownz.array(z.enum())
- - Checkbox multi-selectz.boolean()
- - Checkboxz.number()
- - Number inputz.email()` - Email input
-
MIT