Skip to content

Commit e298305

Browse files
committed
feat: validate -s and -u with regex
1 parent 974bb93 commit e298305

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

commands/daily.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Table from "cli-table3";
33
import { define } from "gunshi";
44
import pc from "picocolors";
55
import { type LoadOptions, loadUsageData } from "../data-loader.ts";
6+
import { parseDateArg } from "../date-validation.ts";
67
import { log, logger } from "../logger.ts";
78
import { formatCurrency, formatNumber } from "../utils.ts";
89

@@ -11,14 +12,16 @@ export const dailyCommand = define({
1112
description: "Show usage report grouped by date",
1213
args: {
1314
since: {
14-
type: "string",
15+
type: "custom",
1516
short: "s",
1617
description: "Filter from date (YYYYMMDD format)",
18+
parse: parseDateArg,
1719
},
1820
until: {
19-
type: "string",
21+
type: "custom",
2022
short: "u",
2123
description: "Filter until date (YYYYMMDD format)",
24+
parse: parseDateArg,
2225
},
2326
path: {
2427
type: "string",

commands/session.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Table from "cli-table3";
33
import { define } from "gunshi";
44
import pc from "picocolors";
55
import { type LoadOptions, loadSessionData } from "../data-loader.ts";
6+
import { parseDateArg } from "../date-validation.ts";
67
import { log, logger } from "../logger.ts";
78
import { formatCurrency, formatNumber } from "../utils.ts";
89

@@ -11,14 +12,16 @@ export const sessionCommand = define({
1112
description: "Show usage report grouped by conversation session",
1213
args: {
1314
since: {
14-
type: "string",
15+
type: "custom",
1516
short: "s",
1617
description: "Filter from date (YYYYMMDD format)",
18+
parse: parseDateArg,
1719
},
1820
until: {
19-
type: "string",
21+
type: "custom",
2022
short: "u",
2123
description: "Filter until date (YYYYMMDD format)",
24+
parse: parseDateArg,
2225
},
2326
path: {
2427
type: "string",

date-validation.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as v from "valibot";
2+
import { dateSchema } from "./types";
3+
4+
export const parseDateArg = (value: string): string => {
5+
const result = v.safeParse(dateSchema, value);
6+
if (!result.success) {
7+
throw new TypeError(result.issues[0].message);
8+
}
9+
return result.output;
10+
};

types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ export type ModelSpec = v.InferOutput<typeof ModelSpecSchema>;
3434
export const LiteLLMModelPricesSchema = v.record(v.string(), ModelSpecSchema);
3535

3636
export type LiteLLMModelPrices = v.InferOutput<typeof LiteLLMModelPricesSchema>;
37+
38+
export const dateSchema = v.pipe(
39+
v.string(),
40+
v.regex(/^\d{8}$/, "Date must be in YYYYMMDD format"),
41+
);

0 commit comments

Comments
 (0)