Skip to content

Commit 2485f34

Browse files
Merge pull request #160 from zereight/feat/153-1
FIX string or number
2 parents df7173a + 40db3f5 commit 2485f34

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

customSchemas.ts

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { z } from "zod";
2+
import { pino } from 'pino';
3+
4+
const logger = pino({
5+
level: process.env.LOG_LEVEL || 'info',
6+
transport: {
7+
target: 'pino-pretty',
8+
options: {
9+
colorize: true,
10+
levelFirst: true,
11+
destination: 2,
12+
},
13+
},
14+
});
215

316
export const flexibleBoolean = z.preprocess((val) => {
417
if (typeof val === 'string') {
@@ -7,21 +20,56 @@ export const flexibleBoolean = z.preprocess((val) => {
720
return val;
821
}, z.boolean());
922

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-
});
23+
24+
export const numericStringSchemaNullable = z.coerce.string().or(z.number()).transform((val, ctx) => {
25+
const strValue = String(val);
26+
const trimmedStrValue = strValue.trim();
27+
28+
if (trimmedStrValue === '') {
29+
ctx.addIssue({
30+
code: z.ZodIssueCode.custom,
31+
message: `Expected a numeric string or number, but received an empty string or string with only whitespace.`,
32+
});
33+
return z.NEVER;
34+
}
35+
if (trimmedStrValue.toLowerCase() === 'null') {
36+
return null;
37+
}
38+
if (isNaN(Number(trimmedStrValue))) {
39+
ctx.addIssue({
40+
code: z.ZodIssueCode.custom,
41+
message: `Expected a numeric string or number, but received ${trimmedStrValue}`,
42+
});
43+
return z.NEVER;
44+
}
45+
return String(trimmedStrValue);
46+
}).nullable();
47+
2648

27-
49+
export const numericStringSchema = z.coerce.string().transform((val, ctx) => {
50+
const strValue = String(val);
51+
const trimmedStrValue = strValue.trim();
52+
53+
if (trimmedStrValue === '') {
54+
ctx.addIssue({
55+
code: z.ZodIssueCode.custom,
56+
message: `Expected a numeric string or number, but received an empty string or string with only whitespace.`,
57+
});
58+
return z.NEVER;
59+
}
60+
if (trimmedStrValue.toLowerCase() === 'null') {
61+
ctx.addIssue({
62+
code: z.ZodIssueCode.custom,
63+
message: `Expected a numeric string or number, but received an null as string`,
64+
});
65+
return z.NEVER;
66+
}
67+
if (isNaN(Number(trimmedStrValue))) {
68+
ctx.addIssue({
69+
code: z.ZodIssueCode.custom,
70+
message: `Expected a numeric string or number, but received ${trimmedStrValue}`,
71+
});
72+
return z.NEVER;
73+
}
74+
return String(trimmedStrValue);
75+
});

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,11 +1076,11 @@ async function listIssues(
10761076
value.forEach(label => {
10771077
url.searchParams.append(`${key}[]`, label.toString());
10781078
});
1079-
} else {
1079+
} else if (value) {
10801080
url.searchParams.append(`${key}[]`, value.toString());
10811081
}
10821082
} else {
1083-
url.searchParams.append(key, value.toString());
1083+
url.searchParams.append(key, String(value));
10841084
}
10851085
}
10861086
});
@@ -1115,7 +1115,7 @@ async function listMergeRequests(
11151115
// Handle array of labels
11161116
url.searchParams.append(key, value.join(","));
11171117
} else {
1118-
url.searchParams.append(key, value.toString());
1118+
url.searchParams.append(key, String(value));
11191119
}
11201120
}
11211121
});

schemas.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import {flexibleBoolean,numericStringSchema} from "./customSchemas.js"
2+
import {flexibleBoolean,numericStringSchema,numericStringSchemaNullable} from "./customSchemas.js"
33

44
// Base schemas for common types
55
export const GitLabAuthorSchema = z.object({
@@ -667,7 +667,7 @@ export const GitLabDiscussionNoteSchema = z.object({
667667
noteable_id: numericStringSchema,
668668
noteable_type: z.enum(["Issue", "MergeRequest", "Snippet", "Commit", "Epic"]),
669669
project_id: z.coerce.string().optional(),
670-
noteable_iid: z.coerce.number().nullable(),
670+
noteable_iid: numericStringSchemaNullable.optional(),
671671
resolvable: flexibleBoolean.optional(),
672672
resolved: flexibleBoolean.optional(),
673673
resolved_by: GitLabUserSchema.nullable().optional(),
@@ -917,14 +917,14 @@ export const CreateNoteSchema = z.object({
917917
noteable_type: z
918918
.enum(["issue", "merge_request"])
919919
.describe("Type of noteable (issue or merge_request)"),
920-
noteable_iid: z.coerce.number().describe("IID of the issue or merge request"),
920+
noteable_iid: numericStringSchema.describe("IID of the issue or merge request"),
921921
body: z.string().describe("Note content"),
922922
});
923923

924924
// Issues API operation schemas
925925
export const ListIssuesSchema = z.object({
926926
project_id: z.coerce.string().describe("Project ID or URL-encoded path"),
927-
assignee_id: numericStringSchema.optional().describe("Return issues assigned to the given user ID"),
927+
assignee_id: z.coerce.string().optional().describe("Return issues assigned to the given user ID. user id or none or any"),
928928
assignee_username: z.array(z.string()).optional().describe("Return issues assigned to the given username"),
929929
author_id: numericStringSchema.optional().describe("Return issues created by the given user ID"),
930930
author_username: z.string().optional().describe("Return issues created by the given username"),
@@ -951,10 +951,7 @@ export const ListIssuesSchema = z.object({
951951
// Merge Requests API operation schemas
952952
export const ListMergeRequestsSchema = z.object({
953953
project_id: z.coerce.string().describe("Project ID or URL-encoded path"),
954-
assignee_id: z
955-
.number()
956-
.optional()
957-
.describe("Returns merge requests assigned to the given user ID"),
954+
assignee_id: z.coerce.string().optional().describe("Return issues assigned to the given user ID. user id or none or any"),
958955
assignee_username: z
959956
.string()
960957
.optional()
@@ -964,10 +961,9 @@ export const ListMergeRequestsSchema = z.object({
964961
.string()
965962
.optional()
966963
.describe("Returns merge requests created by the given username"),
967-
reviewer_id: z
968-
.number()
964+
reviewer_id: z.coerce.string()
969965
.optional()
970-
.describe("Returns merge requests which have the user as a reviewer"),
966+
.describe("Returns merge requests which have the user as a reviewer. user id or none or any"),
971967
reviewer_username: z
972968
.string()
973969
.optional()

0 commit comments

Comments
 (0)