UNPKG

dynamodb-toolbox

Version:

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

17 lines (16 loc) 690 B
import { z } from 'zod'; import { withValidate } from '../utils.js'; import { withDefault, withEncoding, withOptional } from './utils.js'; export const getStringZodParser = (schema, options = {}) => { var _a; let zodFormatter; const { props } = schema; const [enumHead, ...enumTail] = (_a = props.enum) !== null && _a !== void 0 ? _a : []; if (enumHead !== undefined) { zodFormatter = enumTail.length > 0 ? z.enum([enumHead, ...enumTail]) : z.literal(enumHead); } else { zodFormatter = z.string(); } return withEncoding(schema, options, withDefault(schema, options, withOptional(schema, options, withValidate(schema, zodFormatter)))); };