UNPKG

dynamodb-toolbox

Version:

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

41 lines (40 loc) 2.04 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 { SchemaZodParser } from './schema.js'; import type { ZodParserOptions } from './types.js'; import type { WithDefault, WithOptional } from './utils.js'; type WithEncodedKeys<SCHEMA extends RecordSchema, OPTIONS extends ZodParserOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { transform: false; }>, ZOD_SCHEMA, If<Extends<SCHEMA['keys']['props'], { transform: Transformer; }>, z.ZodEffects<ZOD_SCHEMA, TransformedValue<SCHEMA>, z.input<ZOD_SCHEMA>>, ZOD_SCHEMA>>; export declare const compileKeysEncoder: (schema: RecordSchema) => (decoded: unknown) => Record<string, unknown>; export type RecordZodParser<SCHEMA extends RecordSchema, OPTIONS extends ZodParserOptions = {}> = RecordSchema extends SCHEMA ? z.ZodTypeAny : WithDefault<SCHEMA, OPTIONS, 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; }; } ? WithEncodedKeys<SCHEMA, OPTIONS, z.ZodObject<{ [KEY in SCHEMA['keys']['props']['enum'][number]]: SchemaZodParser<SCHEMA['elements'], Overwrite<OPTIONS, { defined: false; }>>; }, 'strip'>> : z.ZodRecord<Cast<SchemaZodParser<SCHEMA['keys'], Overwrite<OPTIONS, { defined: true; }>>, z.KeySchema>, SchemaZodParser<SCHEMA['elements'], Overwrite<OPTIONS, { defined: true; }>>>>>>; export declare const recordZodParser: (schema: RecordSchema, options?: ZodParserOptions) => z.ZodTypeAny; export {};