NapCatQQ/src/common/coerce.ts
手瓜一十雪 0f508a511c fix: coerce
2025-04-17 22:17:35 +08:00

26 lines
659 B
TypeScript

import { z } from 'zod';
const boolean = () => z.preprocess(
val => val === null || val === undefined
? val
: typeof val === 'string' && (val.toLowerCase() === 'false' || val === '0')
? false
: Boolean(val),
z.boolean()
);
const number = () => z.preprocess(
val => val === null || val === undefined
? val
: typeof val !== 'number' ? Number(val) : val,
z.number()
);
const string = () => z.preprocess(
val => val === null || val === undefined
? val
: typeof val !== 'string' ? String(val) : val,
z.string()
);
export const coerce = { boolean, number, string };