dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
14 lines (13 loc) • 642 B
JavaScript
import { z } from 'zod';
import { schemaZodParser } from './schema.js';
import { withAttributeNameEncoding } from './utils.js';
export const itemZodParser = (schema, options = {}) => {
const { mode = 'put' } = options;
const displayedAttrEntries = mode === 'key'
? Object.entries(schema.attributes).filter(([, { props }]) => props.key)
: Object.entries(schema.attributes);
return withAttributeNameEncoding(schema, options, z.object(Object.fromEntries(displayedAttrEntries.map(([attributeName, attribute]) => [
attributeName,
schemaZodParser(attribute, { ...options, defined: false })
]))));
};