-
-
Notifications
You must be signed in to change notification settings - Fork 296
feat: include type in enum
#1351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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']; | ||
| } | ||
| 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
|
||
| break; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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 overvalibotSchema.optionsto 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.