import { Message } from "@optique/core/message"; import * as v from "valibot"; import { NonEmptyString, ValueParser } from "@optique/core/valueparser"; //#region src/index.d.ts /** * Options for creating a Valibot value parser. * @since 0.7.0 */ interface ValibotParserOptions { /** * The metavariable name for this parser. This is used in help messages to * indicate what kind of value this parser expects. Usually a single * word in uppercase, like `VALUE` or `SCHEMA`. * @default `"VALUE"` */ readonly metavar?: NonEmptyString; /** * A phase-one stand-in value of type `T` used during deferred prompt * resolution. Because the output type of a Valibot schema cannot be * inferred to a concrete default, callers must provide this explicitly. * @since 1.0.0 */ readonly placeholder: T; /** * Custom formatter for displaying parsed values in help messages. * When not provided, the default formatter is used: primitives use * `String()`, valid `Date` values use `.toISOString()`, and plain * objects use `JSON.stringify()`. All other objects (arrays, class * instances, etc.) use `String()`. * * @param value The parsed value to format. * @returns A string representation of the value. * @since 1.0.0 */ readonly format?: (value: T) => string; /** * Custom error messages for Valibot validation failures. */ readonly errors?: { /** * Custom error message when input fails Valibot validation. * Can be a static message or a function that receives the Valibot issues * and input string. * @since 0.7.0 */ valibotError?: Message | ((issues: v.InferIssue>>[], input: string) => Message); }; } /** * Creates a value parser from a Valibot schema. * * This parser validates CLI argument strings using Valibot schemas, enabling * powerful validation and transformation capabilities with minimal bundle size * for command-line interfaces. * * The metavar is automatically inferred from the schema type unless explicitly * provided in options. For example: * - `v.string()` → `"STRING"` * - `v.pipe(v.string(), v.email())` → `"EMAIL"` * - `v.number()` → `"NUMBER"` * - `v.pipe(v.number(), v.integer())` → `"INTEGER"` * - `v.picklist([...])` → `"CHOICE"` * * @template T The output type of the Valibot schema. * @param schema A Valibot schema to validate input against. * @param options Configuration for the parser, including a required * `placeholder` value used during deferred prompt resolution. * @returns A value parser that validates inputs using the provided schema. * * @example Basic string validation * ```typescript * import * as v from "valibot"; * import { valibot } from "@optique/valibot"; * import { option } from "@optique/core/primitives"; * * const email = option("--email", * valibot(v.pipe(v.string(), v.email()), { placeholder: "" }), * ); * ``` * * @example Number validation with pipeline * ```typescript * import * as v from "valibot"; * import { valibot } from "@optique/valibot"; * import { option } from "@optique/core/primitives"; * * // Use v.pipe with v.transform for non-string types since CLI args are always strings * const port = option("-p", "--port", * valibot(v.pipe( * v.string(), * v.transform(Number), * v.number(), * v.integer(), * v.minValue(1024), * v.maxValue(65535), * ), { placeholder: 1024 }), * ); * ``` * * @example Picklist validation * ```typescript * import * as v from "valibot"; * import { valibot } from "@optique/valibot"; * import { option } from "@optique/core/primitives"; * * const logLevel = option("--log-level", * valibot(v.picklist(["debug", "info", "warn", "error"]), { * placeholder: "debug", * }), * ); * ``` * * @example Custom error messages * ```typescript * import * as v from "valibot"; * import { valibot } from "@optique/valibot"; * import { message } from "@optique/core/message"; * import { option } from "@optique/core/primitives"; * * const email = option("--email", * valibot(v.pipe(v.string(), v.email()), { * placeholder: "", * metavar: "EMAIL", * errors: { * valibotError: (issues, input) => * message`Please provide a valid email address, got ${input}.` * }, * }), * ); * ``` * * @throws {TypeError} If `options` is missing, not an object, or does not * include `placeholder`. * @throws {TypeError} If the resolved `metavar` is an empty string. * @throws {TypeError} If the schema contains async validations that cannot be * executed synchronously. * @since 0.7.0 */ declare function valibot(schema: v.BaseSchema>, options: ValibotParserOptions): ValueParser<"sync", T>; type AnyValibotSchema = v.BaseSchema> | v.BaseSchemaAsync>; /** * Creates an async value parser from a Valibot schema. * * This parser validates CLI argument strings with Valibot's async safe-parse * path, so schemas using `pipeAsync()`, `checkAsync()`, or other async actions * can be reused directly in asynchronous Optique parsers. * * The metavar, choices, suggestions, formatting, and error customization * follow the same rules as {@link valibot}. Because the returned parser runs * asynchronously, use it with `parseAsync()`, `run()`, or `runAsync()`. * * @template T The output type of the Valibot schema. * @param schema A Valibot schema to validate input against. * @param options Configuration for the parser, including a required * `placeholder` value used during deferred prompt resolution. * @returns An async value parser that validates inputs using the provided * schema. * * @throws {TypeError} If `options` is missing, not an object, or does not * include `placeholder`. * @throws {TypeError} If the resolved `metavar` is an empty string. * @since 1.1.0 */ declare function valibotAsync(schema: AnyValibotSchema, options: ValibotParserOptions): ValueParser<"async", T>; //#endregion export { ValibotParserOptions, valibot, valibotAsync };