UNPKG

dynamodb-toolbox

Version:

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

15 lines (14 loc) 758 B
import { z } from 'zod'; import { withValidate } from '../utils.js'; import { schemaZodFormatter } from './schema.js'; import { withAttributeNameDecoding, withOptional } from './utils.js'; export const mapZodFormatter = (schema, options = {}) => { const { format = true } = options; const displayedAttrEntries = format ? Object.entries(schema.attributes).filter(([, { props }]) => !props.hidden) : Object.entries(schema.attributes); return withAttributeNameDecoding(schema, options, withOptional(schema, options, withValidate(schema, z.object(Object.fromEntries(displayedAttrEntries.map(([attributeName, attribute]) => [ attributeName, schemaZodFormatter(attribute, { ...options, defined: false }) ])))))); };