@cdwr/core
Version:
A set of core utilities for the Codeware ecosystem.
76 lines (75 loc) • 2.09 kB
TypeScript
import { z } from 'zod';
type Options = {
/**
* Dot notation paths that should preserve their values
* without being transformed.
*
* Dot notation is expressed with transformed schema keys.
*
* @example
*
* Typical use case is when the source data contains environment variables
* which we want to preserve as objects.
*
* ```ts
* [ 'machines.config.env' ]
* ```
*/
preserve?: string[];
/**
* Properties that should always be transformed to a specific value.
*
* Map the source key to the desired property key.
*
* @example
*
* Source data contains property `APIKey` which should always be transformed to `apiKey`.
* Default transformation would otherwise turn it into `aPIKey`.
*
* ```ts
* { APIKey: 'apiKey' }
* ```
*/
specialCases?: Record<string, string>;
};
/**
* A Zod preprocessor which will deeply transform the keys of an object to camelCase.
*
* Handles nested objects and arrays.
*
* @example
* ```ts
* const schema = z.object({
* id: z.string(),
* apiKey: z.string(),
* userId: z.string(),
* machines: z.array(
* z.object({
* config: z.object({
* env: z.record(z.string()),
* })
* })
* ),
* machineToken: z.string()
* })
*
* // Default transformation
* const transformed = withCamelCase(schema);
*
* // When `env` contains environment variables, we want to preserve the object as is.
* // Paths should be expressed with dot notation.
* const transformed = withCamelCase(schema, {
* preserve: ['machines.config.env']
* });
*
* // Let's say the source data for api key is `APIKey`, the default transformation would become `apPIey`.
* // Use `specialCases` options to add a custom rule for `apiKey`.
* const transformed = withCamelCase(schema, {
* specialCases: {
* APIKey: 'apiKey'
* }
* });
* ```
*/
export declare const withCamelCase: <T extends z.ZodType>(schema: T, options?: Options) => z.ZodEffects<T, T["_output"], unknown>;
export {};