dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
25 lines (24 loc) • 899 B
JavaScript
import { z } from 'zod';
import { withValidate } from '../utils.js';
import { withDecoding, withOptional } from './utils.js';
export const booleanZodFormatter = (schema, options) => {
var _a;
let zodFormatter;
const { props } = schema;
const [enumHead, ...enumTail] = (_a = props.enum) !== null && _a !== void 0 ? _a : [];
if (enumHead !== undefined) {
const [enumTailHead, ...enumTailTail] = enumTail;
zodFormatter =
enumTailHead !== undefined
? z.union([
z.literal(enumHead),
z.literal(enumTailHead),
...enumTailTail.map(val => z.literal(val))
])
: z.literal(enumHead);
}
else {
zodFormatter = z.boolean();
}
return withDecoding(schema, options, withOptional(schema, options, withValidate(schema, zodFormatter)));
};