dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
15 lines (14 loc) • 718 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);
const zodObject = z.object(Object.fromEntries(displayedAttrEntries.map(([attributeName, attribute]) => [
attributeName,
schemaZodParser(attribute, { ...options, defined: false })
])));
return withAttributeNameEncoding(schema, options, schema.props.strict ? zodObject.strict() : zodObject);
};