UNPKG

dynamodb-toolbox

Version:

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

39 lines (38 loc) 2.74 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 { ZodParserOptions } 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 WithDefault<SCHEMA extends Schema, OPTIONS extends ZodParserOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { fill: false; }>, ZOD_SCHEMA, If<Or<Extends<SCHEMA['props'], { key: true; keyDefault: unknown; }>, Extends<SCHEMA['props'], { key?: false; putDefault: unknown; }>>, z.ZodDefault<ZOD_SCHEMA>, ZOD_SCHEMA>>; export declare const withDefault: (schema: Schema, { fill }: ZodParserOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export type WithOptional<SCHEMA extends Schema, OPTIONS extends ZodParserOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { defined: true; }>, ZOD_SCHEMA, If<Extends<SCHEMA['props'], { required: 'never'; }>, z.ZodOptional<ZOD_SCHEMA>, ZOD_SCHEMA>>; export declare const withOptional: (schema: Schema, { defined }: ZodParserOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export type WithEncoding<SCHEMA extends Schema, OPTIONS extends ZodParserOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Extends<OPTIONS, { transform: false; }>, ZOD_SCHEMA, If<Extends<SCHEMA['props'], { transform: Transformer; }>, z.ZodEffects<ZOD_SCHEMA, TransformedValue<SCHEMA>, z.input<ZOD_SCHEMA>>, ZOD_SCHEMA>>; export declare const withEncoding: (schema: Extract<Schema, { props: { transform?: unknown; }; }>, { transform }: ZodParserOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export type WithAttributeNameEncoding<SCHEMA extends MapSchema | ItemSchema, OPTIONS extends ZodParserOptions, ZOD_SCHEMA extends z.ZodTypeAny> = If<Or<Extends<OPTIONS, { transform: false; }>, Extends<[SavedAsAttributes<SCHEMA>], [never]>>, ZOD_SCHEMA, z.ZodEffects<ZOD_SCHEMA, TransformedValue<SCHEMA>, z.input<ZOD_SCHEMA>>>; export declare const withAttributeNameEncoding: (schema: MapSchema | ItemSchema, { transform }: ZodParserOptions, zodSchema: z.ZodTypeAny) => z.ZodTypeAny; export declare const compileAttributeNameEncoder: (schema: MapSchema | ItemSchema) => (decoded: unknown) => Record<string, unknown>;