/** * @module validators/primitives * @description Primitive validators for the core environment variable types: * strings (with choices/enum support), numbers, and booleans. */ import { z } from "zod/v4"; import type { BooleanOptions, NumberOptions, StringOptions, UndefinedDefault } from "../types.cjs"; /** * String validator with configurable constraints. * Validates environment variables as strings with optional restrictions. * * @param options - Validation options for the string * @returns Zod schema that validates strings * * @example * Basic string validation * ```ts * const env = zenv({ * APP_NAME: str({ default: 'MyApp' }) * }); * ``` * * @example * String with choices (enum) * ```ts * const env = zenv({ * LOG_LEVEL: str({ * choices: ['debug', 'info', 'warn', 'error'], * default: 'info' * }) * }); * ``` * * @example * String with length constraints * ```ts * const env = zenv({ * API_KEY: str({ min: 32, max: 64 }) * }); * ``` * * @example * String with regex pattern * ```ts * const env = zenv({ * VERSION: str({ * regex: /^\d+\.\d+\.\d+$/, * example: '1.2.3' * }) * }); * ``` * * @example * Optional string with undefined default * ```ts * const env = zenv({ * // Optional in all environments * OPTIONAL_KEY: str({ default: undefined }), * // Optional only in dev/test, required in production * STRIPE_KEY: str({ * devDefault: undefined, * testDefault: undefined * }) * }); * // Type: OPTIONAL_KEY is string | undefined * // Type: STRIPE_KEY is string | undefined in dev/test, string in production * ``` * * @example * Client-exposed string with transform * ```ts * const env = zenv({ * INTERNAL_URL: str({ * client: { * expose: true, * transform: (url) => url.replace('internal', 'public') * } * }) * }); * ``` */ export declare function str(options: StringOptions & { choices: TChoices; }): z.ZodType; export declare function str(options: Omit & ({ default: undefined; } | { devDefault: undefined; } | { testDefault: undefined; })): z.ZodType & UndefinedDefault; export declare function str(options?: Omit): z.ZodType; /** * Number validator with automatic string-to-number coercion. * Parses environment variables as numbers with optional constraints. * * @param options - Validation options for the number * @returns Zod number schema with metadata * * @example * Basic number with default * ```ts * const env = zenv({ * PORT: num({ default: 3000 }) * }); * ``` * * @example * Number with min/max range * ```ts * const env = zenv({ * WORKERS: num({ min: 1, max: 100, default: 4 }) * }); * ``` * * @example * Integer validation * ```ts * const env = zenv({ * RETRY_COUNT: num({ int: true, default: 3 }) * }); * ``` * * @example * Positive number only * ```ts * const env = zenv({ * TIMEOUT_MS: num({ positive: true }) * }); * ``` * * @example * Number with choices (enum) * ```ts * const env = zenv({ * LOG_LEVEL_NUM: num({ * choices: [0, 1, 2, 3], * default: 1 * }) * }); * ``` * * @example * Environment-specific defaults * ```ts * const env = zenv({ * POOL_SIZE: num({ * default: 10, * devDefault: 2, * testDefault: 1 * }) * }); * ``` * * @example * Optional number with undefined default * ```ts * const env = zenv({ * // Optional in all environments * CUSTOM_PORT: num({ default: undefined }), * // Optional in dev/test, required in production * MAX_WORKERS: num({ * devDefault: undefined, * testDefault: undefined * }) * }); * // Type: CUSTOM_PORT is number | undefined * // Type: MAX_WORKERS is number | undefined in dev/test, number in production * ``` */ export declare function num(options: NumberOptions & { choices: TChoices; }): z.ZodType; export declare function num(options: Omit & ({ default: undefined; } | { devDefault: undefined; } | { testDefault: undefined; })): z.ZodType & UndefinedDefault; export declare function num(options?: Omit): z.ZodType; /** * Boolean validator with precise string-to-boolean parsing. * Handles common boolean string representations accurately. * * @param options - Validation options for the boolean * @returns Zod schema that outputs boolean * * @example * Basic boolean with default * ```ts * const env = zenv({ * DEBUG: bool({ default: false }) * }); * ``` * * @example * Parsing various boolean strings * ```ts * // All of these parse correctly: * // "true", "false", "1", "0", "yes", "no", "on", "off" * const env = zenv({ * FEATURE_ENABLED: bool(), * USE_CACHE: bool(), * VERBOSE: bool() * }); * ``` * * @example * Environment-specific defaults * ```ts * const env = zenv({ * ENABLE_TELEMETRY: bool({ * default: true, * devDefault: false, * testDefault: false * }) * }); * ``` * * @example * Client-exposed boolean * ```ts * const env = zenv({ * SHOW_DEBUG_UI: bool({ * client: { * expose: true, * default: false, * devDefault: true * } * }) * }); * ``` * * @example * Optional boolean with undefined default * ```ts * const env = zenv({ * // Optional in all environments * EXPERIMENTAL_FEATURE: bool({ default: undefined }), * // Optional in dev/test, required in production * ENABLE_MONITORING: bool({ * devDefault: undefined, * testDefault: undefined * }) * }); * // Type: EXPERIMENTAL_FEATURE is boolean | undefined * // Type: ENABLE_MONITORING is boolean | undefined in dev/test, boolean in production * ``` */ export declare function bool(options: BooleanOptions & ({ default: undefined; } | { devDefault: undefined; } | { testDefault: undefined; })): z.ZodType & UndefinedDefault; export declare function bool(options?: BooleanOptions): z.ZodType;