Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/cli/src/config/settings-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,66 @@ describe('settings-validation', () => {
});
});

describe('env variable string coercion', () => {
it('should coerce string "true" to boolean true', () => {
const settings = {
ui: { autoThemeSwitching: 'true' as unknown as boolean },
};
const result = validateSettings(settings);
expect(result.success).toBe(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.data as any).ui.autoThemeSwitching).toBe(true);
});

it('should coerce string "false" to boolean false', () => {
const settings = {
ui: { autoThemeSwitching: 'false' as unknown as boolean },
};
const result = validateSettings(settings);
expect(result.success).toBe(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.data as any).ui.autoThemeSwitching).toBe(false);
});

it('should handle case-insensitive "TRUE"/"FALSE"', () => {
const settings = {
ui: { autoThemeSwitching: 'TRUE' as unknown as boolean },
};
const result = validateSettings(settings);
expect(result.success).toBe(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.data as any).ui.autoThemeSwitching).toBe(true);
});

it('should trim whitespace around boolean strings', () => {
const settings = {
ui: { autoThemeSwitching: ' true ' as unknown as boolean },
};
const result = validateSettings(settings);
expect(result.success).toBe(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.data as any).ui.autoThemeSwitching).toBe(true);
});

it('should still reject non-boolean strings like "yes"', () => {
const settings = {
ui: { autoThemeSwitching: 'yes' as unknown as boolean },
};
const result = validateSettings(settings);
expect(result.success).toBe(false);
});

it('should coerce numeric strings to numbers', () => {
const settings = {
model: { maxSessionTurns: '50' as unknown as number },
};
const result = validateSettings(settings);
expect(result.success).toBe(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result.data as any).model.maxSessionTurns).toBe(50);
});
});

describe('formatValidationError', () => {
it('should format error with file path and helpful message for model.name', () => {
const invalidSettings = {
Expand Down
31 changes: 27 additions & 4 deletions packages/cli/src/config/settings-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ function buildZodSchemaFromJsonSchema(def: any): z.ZodTypeAny {
if (def.enum) return z.enum(def.enum as [string, ...string[]]);
return z.string();
}
if (def.type === 'number') return z.number();
if (def.type === 'boolean') return z.boolean();
if (def.type === 'number')
return z.preprocess(coerceNumberString, z.number());
if (def.type === 'boolean')
return z.preprocess(coerceBooleanString, z.boolean());

if (def.type === 'array') {
if (def.items) {
Expand Down Expand Up @@ -123,6 +125,27 @@ function buildObjectShapeFromProperties(
return shape;
}

// Env vars like "${GEMINI_AUTO_THEME:-true}" resolve to the string "true"
// but Zod expects an actual boolean. Same deal with numbers.
function coerceBooleanString(val: unknown): unknown {
if (typeof val === 'string') {
const trimmed = val.trim().toLowerCase();
if (trimmed === 'true') return true;
if (trimmed === 'false') return false;
}
return val;
}

function coerceNumberString(val: unknown): unknown {
if (typeof val === 'string') {
const trimmed = val.trim();
if (trimmed !== '' && !isNaN(Number(trimmed))) {
return Number(trimmed);
}
}
return val;
}

/**
* Builds a Zod schema for primitive types (string, number, boolean)
*/
Expand All @@ -133,9 +156,9 @@ function buildPrimitiveSchema(
case 'string':
return z.string();
case 'number':
return z.number();
return z.preprocess(coerceNumberString, z.number());
case 'boolean':
return z.boolean();
return z.preprocess(coerceBooleanString, z.boolean());
default:
return z.unknown();
}
Expand Down