UNPKG

dynamodb-toolbox

Version:

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

31 lines (30 loc) 2.33 kB
import { z } from 'zod'; import type { ItemSchema, MapSchema, Schema, TransformedValue } from '../../../../schema/index.js'; import type { Transformer } from '../../../../transformers/transformer.js'; import type { Extends, If, Or } from '../../../../types/index.js'; import type { SavedAsAttributes } from '../utils.js'; import type { ZodFormatterOptions } from './types.js'; export type ZodLiteralMap<LITERALS extends z.Primitive[], RESULTS extends z.ZodLiteral<z.Primitive>[] = []> = LITERALS extends [infer LITERALS_HEAD, ...infer LITERALS_TAIL] ? LITERALS_HEAD extends z.Primitive ? LITERALS_TAIL extends z.Primitive[] ? ZodLiteralMap<LITERALS_TAIL, [...RESULTS, z.ZodLiteral<LITERALS_HEAD>]> : never : never : RESULTS; export type WithOptional<SCHEMA extends Schema, OPTIONS extends ZodFormatterOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { defined: true; }>, ZOD_SCHEMA, If<Or<Extends<OPTIONS, { partial: true; }>, Extends<SCHEMA['props'], { required: 'never'; }>>, z.ZodOptional<ZOD_SCHEMA>, ZOD_SCHEMA>>; export declare const withOptional: (schema: Schema, { partial, defined }: ZodFormatterOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export type WithDecoding<SCHEMA extends Schema, OPTIONS extends ZodFormatterOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { transform: false; }>, ZOD_SCHEMA, If<Extends<SCHEMA['props'], { transform: Transformer; }>, z.ZodEffects<ZOD_SCHEMA, z.output<ZOD_SCHEMA>, TransformedValue<SCHEMA>>, ZOD_SCHEMA>>; export declare const withDecoding: (schema: Extract<Schema, { props: { transform?: unknown; }; }>, { transform }: ZodFormatterOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export type WithAttributeNameDecoding<SCHEMA extends MapSchema | ItemSchema, OPTIONS extends ZodFormatterOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Or<Extends<OPTIONS, { transform: false; }>, Extends<[SavedAsAttributes<SCHEMA>], [never]>>, ZOD_SCHEMA, z.ZodEffects<ZOD_SCHEMA, z.output<ZOD_SCHEMA>, TransformedValue<SCHEMA>>>; export declare const withAttributeNameDecoding: (schema: MapSchema | ItemSchema, { transform }: ZodFormatterOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export declare const compileAttributeNameDecoder: (schema: MapSchema | ItemSchema) => (encoded: unknown) => Record<string, unknown>;