Files
Artveris/apps/api/src/common/utils/query-boolean.util.ts
T

13 lines
592 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { TransformFnParams } from 'class-transformer';
/**
* 解析查询参数里的布尔值,配合 @Transform 使用。
* 全局校验管道开启了隐式类型转换,查询字符串 'false' 会先被转成 true 再交给 @Transform,
* 所以这里读取转换前的原始值;未传或传空时保持未设置,不当作 false。
*/
export function queryBoolean({ obj, key }: TransformFnParams): boolean | undefined {
const raw: unknown = obj?.[key];
if (raw === undefined || raw === null || raw === '') return undefined;
return raw === true || raw === 'true';
}