Skip to content

Commit feead60

Browse files
committed
chore: add tests and pre-processor for other cases
1 parent 370ad81 commit feead60

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

src/common/config/configUtils.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ export function commaSeparatedToArray<T extends string[]>(str: string | string[]
6969
return undefined;
7070
}
7171

72-
if (!Array.isArray(str)) {
73-
return [str] as T;
72+
if (typeof str === "string") {
73+
return str
74+
.split(",")
75+
.map((e) => e.trim())
76+
.filter((e) => e.length > 0) as T;
7477
}
7578

7679
if (str.length === 1) {
@@ -82,3 +85,24 @@ export function commaSeparatedToArray<T extends string[]>(str: string | string[]
8285

8386
return str as T;
8487
}
88+
89+
/**
90+
* Preprocessor for boolean values that handles string "false"/"0" correctly.
91+
* Zod's coerce.boolean() treats any non-empty string as true, which is not what we want.
92+
*/
93+
export function parseBoolean(val: unknown): unknown {
94+
if (typeof val === "string") {
95+
const lower = val.toLowerCase().trim();
96+
if (lower === "false" || lower === "") {
97+
return false;
98+
}
99+
return true;
100+
}
101+
if (typeof val === "boolean") {
102+
return val;
103+
}
104+
if (typeof val === "number") {
105+
return val !== 0;
106+
}
107+
return !!val;
108+
}

src/common/config/userConfig.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { z as z4 } from "zod/v4";
22
import { type CliOptions } from "@mongosh/arg-parser";
3-
import { type ConfigFieldMeta, commaSeparatedToArray, getExportsPath, getLogPath } from "./configUtils.js";
3+
import {
4+
type ConfigFieldMeta,
5+
commaSeparatedToArray,
6+
getExportsPath,
7+
getLogPath,
8+
parseBoolean,
9+
} from "./configUtils.js";
410
import { previewFeatureValues, similarityValues } from "../schemas.js";
511

612
// TODO: UserConfig should only be UserConfigSchema and not an intersection with
@@ -68,13 +74,13 @@ export const UserConfigSchema = z4.object({
6874
"An array of tool names that require user confirmation before execution. Requires the client to support elicitation."
6975
),
7076
readOnly: z4
71-
.boolean()
77+
.preprocess(parseBoolean, z4.boolean())
7278
.default(false)
7379
.describe(
7480
"When set to true, only allows read, connect, and metadata operation types, disabling create/update/delete operations."
7581
),
7682
indexCheck: z4
77-
.boolean()
83+
.preprocess(parseBoolean, z4.boolean())
7884
.default(false)
7985
.describe(
8086
"When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan."
@@ -149,7 +155,7 @@ export const UserConfigSchema = z4.object({
149155
)
150156
.register(configRegistry, { isSecret: true }),
151157
disableEmbeddingsValidation: z4
152-
.boolean()
158+
.preprocess(parseBoolean, z4.boolean())
153159
.default(false)
154160
.describe("When set to true, disables validation of embeddings dimensions."),
155161
vectorSearchDimensions: z4.coerce

tests/unit/common/config.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,44 @@ describe("config", () => {
450450
cli: ["--version"],
451451
expected: { version: true },
452452
},
453+
{
454+
cli: ["--readOnly"],
455+
expected: { readOnly: true },
456+
},
457+
{
458+
cli: ["--readOnly", "false"],
459+
expected: { readOnly: false },
460+
},
461+
{
462+
cli: ["--readOnly", "FALSE"],
463+
// This is yargs-parser default
464+
expected: { readOnly: true },
465+
},
466+
{
467+
cli: ["--readOnly", "0"],
468+
// This is yargs-parser default
469+
expected: { readOnly: true },
470+
},
471+
{
472+
cli: ["--readOnly", "1"],
473+
expected: { readOnly: true },
474+
},
475+
{
476+
cli: ["--readOnly", "true"],
477+
expected: { readOnly: true },
478+
},
479+
{
480+
cli: ["--readOnly", "yes"],
481+
expected: { readOnly: true },
482+
},
483+
{
484+
cli: ["--readOnly", "no"],
485+
expected: { readOnly: true },
486+
},
487+
{
488+
cli: ["--readOnly", ""],
489+
expected: { readOnly: true },
490+
},
453491
] as { cli: string[]; expected: Partial<UserConfig> }[];
454492

455493
for (const { cli, expected } of testCases) {

0 commit comments

Comments
 (0)