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
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,43 @@ describe('convertSchema', () => {
createContext()
)
).toStrictEqual({
type: ['string', 'number'],
enum: [0, 1, 'foo', 123],
});

enum TestOnlyNumbersEnum {
KEY1,
KEY2,
}
expect(
convertSchema(
{},
// @ts-expect-error
v.enum(TestOnlyNumbersEnum),
undefined,
createContext()
)
).toStrictEqual({
type: 'number',
enum: [0, 1],
});

enum TestOnlyStringsEnum {
KEY1 = 'key1',
KEY2 = 'key2',
}
expect(
convertSchema(
{},
// @ts-expect-error
v.enum(TestOnlyStringsEnum),
undefined,
createContext()
)
).toStrictEqual({
type: 'string',
enum: ['key1', 'key2'],
});
});

test('should convert supported picklist schema', () => {
Expand All @@ -812,7 +847,29 @@ describe('convertSchema', () => {
undefined,
createContext()
)
).toStrictEqual({ enum: ['foo', 123, 'bar', 456] });
).toStrictEqual({
type: ['string', 'number'],
enum: ['foo', 123, 'bar', 456],
});

expect(
convertSchema(
{},
v.picklist(['foo', 'bar']),
undefined,
createContext()
)
).toStrictEqual({
type: 'string',
enum: ['foo', 'bar'],
});

expect(
convertSchema({}, v.picklist([123, 456]), undefined, createContext())
).toStrictEqual({
type: 'number',
enum: [123, 456],
});
});

test('should throw error for unsupported picklist schema', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,22 +428,45 @@ export function convertSchema(

case 'enum': {
jsonSchema.enum = valibotSchema.options;
// the type could technically always be an array with only `string` or `number`
// we are specifically doing this check to match the MCP expectations that an enum
// can only contain string values AND the type is exactly "string" instead of ["string"]
if (valibotSchema.options.every((option) => typeof option === 'string')) {
jsonSchema.type = 'string';
} else if (
valibotSchema.options.every((option) => typeof option === 'number')
) {
jsonSchema.type = 'number';
} else {
jsonSchema.type = ['string', 'number'];
}
Comment on lines +434 to +442
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code performs two separate every() iterations over valibotSchema.options to determine the type. In the worst case (mixed string/number enum), it checks all elements twice. Consider optimizing to a single pass through the array.

Copilot uses AI. Check for mistakes.
break;
}

case 'picklist': {
if (
valibotSchema.options.some(
(option) => typeof option !== 'number' && typeof option !== 'string'
)
) {
const invalid = valibotSchema.options.some(
(option) => typeof option !== 'number' && typeof option !== 'string'
);
if (invalid) {
errors = addError(
errors,
'An option of the "picklist" schema is not JSON compatible.'
);
}
// @ts-expect-error
jsonSchema.enum = valibotSchema.options;
// the type could technically always be an array with only `string` or `number`
// we are specifically doing this check to match the MCP expectations that an enum
// can only contain string values AND the type is exactly "string" instead of ["string"]
if (valibotSchema.options.every((option) => typeof option === 'string')) {
jsonSchema.type = 'string';
} else if (
valibotSchema.options.every((option) => typeof option === 'number')
) {
jsonSchema.type = 'number';
} else if (!invalid) {
jsonSchema.type = ['string', 'number'];
}
Comment on lines +447 to +469
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code performs up to three separate iterations over valibotSchema.options: one some() check at line 447 and two every() checks at lines 461 and 464. This could be optimized to a single pass through the array, especially since we're checking the same typeof conditions. Consider combining the validation and type determination into one loop.

Copilot uses AI. Check for mistakes.
Comment on lines +431 to +469
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the type is duplicated between the enum and picklist cases. Consider extracting this into a helper function to reduce code duplication and improve maintainability.

Example:

function determineEnumType(options: unknown[], invalid = false): string | string[] | undefined {
  if (invalid) return undefined;
  if (options.every((option) => typeof option === 'string')) {
    return 'string';
  } else if (options.every((option) => typeof option === 'number')) {
    return 'number';
  } else {
    return ['string', 'number'];
  }
}

Then use it in both cases:

const enumType = determineEnumType(valibotSchema.options);
if (enumType) jsonSchema.type = enumType;

Copilot uses AI. Check for mistakes.
break;
}

Expand Down
Loading