Skip to content

Commit df7173a

Browse files
Merge pull request #158 from zereight/feat/153-1
FIX: id string or number
2 parents 2b6863a + 0a3b206 commit df7173a

File tree

2 files changed

+136
-117
lines changed

2 files changed

+136
-117
lines changed

customSchemas.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { z } from "zod";
2+
3+
export const flexibleBoolean = z.preprocess((val) => {
4+
if (typeof val === 'string') {
5+
return val.toLowerCase() === 'true';
6+
}
7+
return val;
8+
}, z.boolean());
9+
10+
export const numericStringSchema = z.string().or(z.number()).transform((val, ctx) => {
11+
let strValue: string;
12+
if (typeof val === 'number') {
13+
strValue = String(val);
14+
} else {
15+
strValue = val;
16+
}
17+
if (strValue.trim() === '' || isNaN(Number(strValue))) {
18+
ctx.addIssue({
19+
code: z.ZodIssueCode.custom,
20+
message: `Expected a numeric string or number, but received "${val}"`,
21+
});
22+
return z.NEVER;
23+
}
24+
return strValue;
25+
});
26+
27+

0 commit comments

Comments
 (0)