UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

41 lines (40 loc) 2.03 kB
import { z } from 'zod'; import type { RecordSchema, TransformedValue } from '../../../../schema/index.js'; import type { Transformer } from '../../../../transformers/transformer.js'; import type { Cast } from '../../../../types/cast.js'; import type { Extends, If } from '../../../../types/index.js'; import type { Overwrite } from '../../../../types/overwrite.js'; import type { WithValidate } from '../utils.js'; import type { SchemaZodFormatter } from './schema.js'; import type { ZodFormatterOptions } from './types.js'; import type { WithOptional } from './utils.js'; type WithDecodedKeys<SCHEMA extends RecordSchema, OPTIONS extends ZodFormatterOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { transform: false; }>, ZOD_SCHEMA, If<Extends<SCHEMA['keys']['props'], { transform: Transformer; }>, z.ZodEffects<ZOD_SCHEMA, z.output<ZOD_SCHEMA>, TransformedValue<SCHEMA>>, ZOD_SCHEMA>>; export declare const compileKeysDecoder: (schema: RecordSchema) => (encoded: unknown) => Record<string, unknown>; export type RecordZodFormatter<SCHEMA extends RecordSchema, OPTIONS extends ZodFormatterOptions = {}> = RecordSchema extends SCHEMA ? z.ZodTypeAny : WithOptional<SCHEMA, OPTIONS, WithValidate<SCHEMA, /** * @debt dependency "Using ZodObject until ZodStrictRecord is a thing: https://github.com/colinhacks/zod/issues/2623" */ SCHEMA extends { keys: { props: { enum: string[]; }; }; props: { partial?: false; }; } ? WithDecodedKeys<SCHEMA, OPTIONS, z.ZodObject<{ [KEY in SCHEMA['keys']['props']['enum'][number]]: SchemaZodFormatter<SCHEMA['elements'], Overwrite<OPTIONS, { defined: false; }>>; }, 'strip'>> : z.ZodRecord<Cast<SchemaZodFormatter<SCHEMA['keys'], Overwrite<OPTIONS, { defined: true; }>>, z.KeySchema>, SchemaZodFormatter<SCHEMA['elements'], Overwrite<OPTIONS, { defined: true; }>>>>>; export declare const recordZodFormatter: (schema: RecordSchema, options?: ZodFormatterOptions) => z.ZodTypeAny; export {};