@withstudiocms/config-utils
Version:
Utilities for managing configuration files
41 lines (40 loc) • 2.18 kB
TypeScript
import { z } from 'astro/zod';
/**
* Parses and validates the provided configuration options using the given Zod schema.
*
* @template T - A Zod schema type extending `z.ZodTypeAny`.
* @param schema - The Zod schema to validate the configuration options against.
* @param opts - The configuration options to be validated.
* @returns The validated and parsed configuration options as the schema's output type.
* @throws {Error} If the configuration options are invalid or if an unknown error occurs during parsing.
*/
export declare function parseConfig<T extends z.ZodTypeAny>(schema: T, opts: unknown): T['_output'];
/**
* Recursively removes all default values from a given Zod schema.
*
* - For `ZodDefault`, it unwraps the default value.
* - For `ZodObject`, it processes each field and wraps them as optional without defaults.
* - For `ZodArray`, `ZodOptional`, `ZodNullable`, and `ZodTuple`, it recursively processes their elements/items.
* - For all other schema types, it returns the schema as-is.
*
* @param schema - The Zod schema to process.
* @returns A new Zod schema with all defaults removed and fields made optional where applicable.
*/
export declare function deepRemoveDefaults(schema: z.ZodTypeAny): z.ZodTypeAny;
/**
* Parses and merges configuration objects using a Zod schema.
*
* This function removes all default values from the provided schema,
* parses the `configFile` object with the modified schema, and then
* deeply merges the result with the `inlineConfig` object.
*
* If parsing fails, an error is thrown with a descriptive message.
*
* @typeParam T - A Zod schema type.
* @param schema - The Zod schema to use for validation.
* @param inlineConfig - The inline configuration object, expected to match the schema's output type.
* @param configFile - The configuration object to parse, expected to match the schema's input type.
* @returns The merged configuration object, conforming to the schema's output type.
* @throws {Error} If parsing the configuration fails.
*/
export declare function parseAndMerge<T extends z.ZodTypeAny>(schema: T, inlineConfig?: T['_output'], configFile?: T['_input']): T['_output'];