Skip to content

Commit b31ba72

Browse files
Merge pull request #201 from zereight/feat/199-1
FIX: flexible boolean
2 parents ac53f08 + 19b7254 commit b31ba72

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

customSchemas.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ const logger = pino({
1515
},
1616
});
1717

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

2531
export const flexibleBooleanNullable = DEFAULT_NULL ? flexibleBoolean.nullable().default(null) : flexibleBoolean.nullable();

0 commit comments

Comments
 (0)