UNPKG

dynamodb-toolbox

Version:

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

16 lines (15 loc) 871 B
import { z } from 'zod'; import { withValidate } from '../utils.js'; import { schemaZodParser } from './schema.js'; import { withAttributeNameEncoding, withDefault, withOptional } from './utils.js'; export const mapZodParser = (schema, options = {}) => { const { mode = 'put' } = options; const displayedAttrEntries = mode === 'key' ? Object.entries(schema.attributes).filter(([, { props }]) => props.key) : Object.entries(schema.attributes); const zodObject = z.object(Object.fromEntries(displayedAttrEntries.map(([attributeName, attribute]) => [ attributeName, schemaZodParser(attribute, { ...options, defined: false }) ]))); return withAttributeNameEncoding(schema, options, withDefault(schema, options, withOptional(schema, options, withValidate(schema, schema.props.strict ? zodObject.strict() : zodObject)))); };