-
Notifications
You must be signed in to change notification settings - Fork 2
[UPDATE PRIMITIVE] Normalize camelCase params to kebab-case with actionable error messages for CLI tools #224
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13b4ddb
Initial plan
Copilot b5ceba5
feat: improve error messages for unrecognized CLI tool params and accβ¦
Copilot 92af8e0
[UPDATE PRIMITIVE] Fix ZodEffects schema rejection in CLI tool registβ¦
Copilot 28a6243
fix: capture suggestion alongside unknownEntries for "did you mean?" β¦
Copilot 5a25ffe
More fixes for PR review feedback
data-douser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Parameter normalization utilities for CLI tool schemas. | ||
| * | ||
| * Provides camelCase β kebab-case key normalization and | ||
| * "did you mean?" suggestions for unrecognized property names. | ||
| */ | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| // βββ String-case conversion helpers ββββββββββββββββββββββββββββββββββββββββββ | ||
|
|
||
| /** | ||
| * Convert a camelCase string to kebab-case. | ||
| * Example: "sourceRoot" β "source-root" | ||
| */ | ||
| export function camelToKebabCase(key: string): string { | ||
| return key.replace(/[A-Z]/g, (ch) => '-' + ch.toLowerCase()); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a kebab-case string to camelCase. | ||
| * Example: "source-root" β "sourceRoot" | ||
| */ | ||
| export function kebabToCamelCase(key: string): string { | ||
| return key.replace(/-([a-z])/g, (_match, ch: string) => ch.toUpperCase()); | ||
| } | ||
|
|
||
| // βββ Suggestion logic ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
|
|
||
| /** | ||
| * Given an unrecognized property name and the set of known schema keys, | ||
| * return the most likely intended key (or `undefined` if no close match). | ||
| * | ||
| * Resolution order: | ||
| * 1. camelCase β kebab-case (e.g. "sourceRoot" β "source-root") | ||
| * 2. snake_case β kebab-case (e.g. "source_root" β "source-root") | ||
| * 3. kebab-case β camelCase (e.g. "source-root" β "sourceRoot") | ||
| */ | ||
| export function suggestPropertyName( | ||
| key: string, | ||
| knownKeys: ReadonlySet<string>, | ||
| ): string | undefined { | ||
| // 1. camelCase β kebab-case | ||
| const kebab = camelToKebabCase(key); | ||
| if (kebab !== key && knownKeys.has(kebab)) return kebab; | ||
|
|
||
| // 2. snake_case β kebab-case | ||
| const snakeToKebab = key.replace(/_/g, '-'); | ||
| if (snakeToKebab !== key && knownKeys.has(snakeToKebab)) return snakeToKebab; | ||
|
|
||
| // 3. kebab-case β camelCase | ||
| const camel = kebabToCamelCase(key); | ||
| if (camel !== key && knownKeys.has(camel)) return camel; | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| // βββ Schema builder ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
|
|
||
| /** | ||
| * Build an enhanced Zod schema from a raw tool input shape. | ||
| * | ||
| * The returned schema: | ||
| * - Accepts additional (unknown) properties without client-side rejection | ||
| * (`passthrough` mode β JSON Schema `additionalProperties: true`). | ||
| * - Normalizes camelCase / snake_case keys to their kebab-case equivalents | ||
| * when a matching schema key exists. | ||
| * - Rejects truly unknown properties with a helpful error that names the | ||
| * unrecognized key and, when possible, suggests the correct name. | ||
| */ | ||
| export function buildEnhancedToolSchema( | ||
| shape: Record<string, z.ZodTypeAny>, | ||
| ): z.ZodTypeAny { | ||
| const knownKeys = new Set(Object.keys(shape)); | ||
|
|
||
| return z | ||
| .object(shape) | ||
| .passthrough() | ||
| .transform((data, ctx) => { | ||
| const normalized: Record<string, unknown> = {}; | ||
| const unknownEntries: Array<{ key: string; hint?: string; isDuplicate: boolean }> = []; | ||
|
|
||
| for (const [key, value] of Object.entries(data)) { | ||
| if (knownKeys.has(key)) { | ||
| // Known key β keep as-is | ||
| normalized[key] = value; | ||
| } else { | ||
| // Try to find a kebab-case equivalent | ||
| const suggestion = suggestPropertyName(key, knownKeys); | ||
| if (suggestion && !(suggestion in data) && !(suggestion in normalized)) { | ||
| // Silently normalize to the canonical kebab-case key | ||
| normalized[suggestion] = value; | ||
| } else { | ||
| // Either no suggestion (truly unknown) or the canonical key is | ||
| // already present. Capture the suggestion so the error message | ||
| // can include a helpful hint. | ||
| const isDuplicate = !!suggestion && (suggestion in data || suggestion in normalized); | ||
| unknownEntries.push({ key, hint: suggestion, isDuplicate }); | ||
| } | ||
data-douser marked this conversation as resolved.
Show resolved
Hide resolved
data-douser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // Report unknown / duplicate properties with actionable messages | ||
| for (const { key, hint, isDuplicate } of unknownEntries) { | ||
| const message = isDuplicate && hint | ||
| ? `duplicate property: both '${key}' and its canonical form '${hint}' were provided; use only '${hint}'` | ||
| : hint | ||
| ? `unknown property '${key}' β did you mean '${hint}'?` | ||
| : `unknown property '${key}'`; | ||
| ctx.addIssue({ | ||
| code: z.ZodIssueCode.custom, | ||
| message, | ||
| path: [key], | ||
| }); | ||
| } | ||
|
|
||
| if (unknownEntries.length > 0) { | ||
| return z.NEVER; | ||
| } | ||
|
|
||
| return normalized; | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.