Skip to content

Commit 42ded6e

Browse files
author
iwakitakuma
committed
FIX: flexible boolean
issue: #199
1 parent ac53f08 commit 42ded6e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

customSchemas.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { z } from "zod";
2-
import { pino } from 'pino';
2+
import { pino } from "pino";
33
const DEFAULT_NULL = process.env.DEFAULT_NULL === "true";
44

5-
65
const logger = pino({
7-
level: process.env.LOG_LEVEL || 'info',
6+
level: process.env.LOG_LEVEL || "info",
87
transport: {
9-
target: 'pino-pretty',
8+
target: "pino-pretty",
109
options: {
1110
colorize: true,
1211
levelFirst: true,
@@ -15,11 +14,19 @@ const logger = pino({
1514
},
1615
});
1716

18-
export const flexibleBoolean = z.preprocess((val) => {
19-
if (typeof val === 'string') {
20-
return val.toLowerCase() === 'true';
21-
}
17+
export const flexibleBoolean = z.preprocess(val => {
18+
if (typeof val === "boolean") {
2219
return val;
20+
}
21+
let result = "false";
22+
try {
23+
result = String(val).toLowerCase();
24+
} catch {
25+
return false;
26+
}
27+
return result in ["true", "t", "1"];
2328
}, z.boolean());
2429

25-
export const flexibleBooleanNullable = DEFAULT_NULL ? flexibleBoolean.nullable().default(null) : flexibleBoolean.nullable();
30+
export const flexibleBooleanNullable = DEFAULT_NULL
31+
? flexibleBoolean.nullable().default(null)
32+
: flexibleBoolean.nullable();

0 commit comments

Comments
 (0)