-
-
Notifications
You must be signed in to change notification settings - Fork 420
feat: allow extending TypeBox errors #1613
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?
Conversation
WalkthroughReplaces the fixed union type Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related issues
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)src/type-system/types.ts (1)
🔇 Additional comments (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/type-system/types.ts (1)
149-156: Interface-based map cleanly exposes an augmentable custom-error surfaceThis refactor looks solid:
ElysiaTypeCustomErrorsas an exported interface plusElysiaTypeCustomError = ElysiaTypeCustomErrors[keyof ElysiaTypeCustomErrors]keeps the effective default union (string | boolean | number | ElysiaTypeCustomErrorCallback) while enabling consumers to extend the union via interface augmentation. The(string & {})on thestringmember is also consistent with the existingFileTypepattern and should preserve autocomplete when plugins add template-literal string types while still allowing arbitrary strings.One optional improvement: consider adding a brief JSDoc comment above
ElysiaTypeCustomErrorsthat shows the intended augmentation pattern, e.g.:/** * Augment this interface to extend allowed values for SchemaOptions['error']: * * declare module 'elysia' { * interface ElysiaTypeCustomErrors { * myPlugin: 'my.plugin.error' | `my.plugin.${string}` * } * } */ export interface ElysiaTypeCustomErrors { ... }That would make the extension story discoverable without needing to read the PR or docs.
Please double-check that your intended augmentation examples type-check as expected in a consumer project (especially around editor autocomplete for string templates).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/type-system/types.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/type-system/types.ts (1)
src/index.ts (2)
ElysiaTypeCustomErrorCallback(8142-8142)ElysiaTypeCustomError(8141-8141)
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/type-system/types.ts (1)
163-168: Consider more descriptive property names for clarity.The interface property names (
string,boolean,number,ElysiaTypeCustomErrorCallback) serve as arbitrary keys in the mapped type pattern. While functional, they could be more descriptive (e.g.,stringErrors,booleanErrors, etc.) to clarify their purpose as extension points rather than primitive type names.However, since the current naming is concise and the pattern works correctly, this is a minor improvement suggestion.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/type-system/types.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/type-system/types.ts (1)
src/index.ts (2)
ElysiaTypeCustomErrorCallback(8142-8142)ElysiaTypeCustomError(8141-8141)
🔇 Additional comments (3)
src/type-system/types.ts (3)
149-162: Excellent documentation for module augmentation.The JSDoc comment clearly explains how to extend error types and provides a practical example. This will help users understand how to add their own custom error templates.
170-170: Excellent use of indexed type for extensibility.The indexed lookup pattern
ElysiaTypeCustomErrors[keyof ElysiaTypeCustomErrors]correctly enables module augmentation while maintaining backwards compatibility. The resulting union type is equivalent to the original with the added benefit of extensibility.
216-220: No conflict with TypeBox's SchemaOptions interface.
@sinclair/typebox'sSchemaOptionsdoes not define anerrorproperty, so this augmentation safely extends it without conflicts. The index signature in TypeBox'sSchemaOptions([prop: string]: any) allows this custom property.Note: TypeBox's recommended approach for custom validation messages is using
SetErrorFunctionorDefaultErrorFunctionrather than a schema-level property, but your augmentation is compatible and functional.
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/type-system/types.ts (1)
149-175: Extensible error type design looks solid; consider a small doc clarificationThe new
ElysiaTypeCustomErrorsinterface plusElysiaTypeCustomError = ElysiaTypeCustomErrors[keyof ElysiaTypeCustomErrors]cleanly enables module augmentation while keeping the default union (string & {}| boolean | number | callback) effectively backwards compatible. Usingstring & {}here is consistent with the rest of the file and should preserve template‑literal autocomplete without restricting arbitrary strings.One small suggestion: in the JSDoc example, ensure the module specifier in
declare module 'elysia/type-system/types'exactly matches the module name that actually exportsElysiaTypeCustomErrorsin the published.d.ts. If the public entry point re‑exports this type from'elysia'(or another path), reflecting that in the example would make augmentation easier for consumers.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/type-system/types.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/type-system/types.ts (1)
src/index.ts (2)
ElysiaTypeCustomErrorCallback(8142-8142)ElysiaTypeCustomError(8141-8141)
Solves #1612 by exposing a globally augmentable interface for adding custom error messages, and using
string & {}to allow any string while preserving autocomplete for string templates.Summary by CodeRabbit
Improvements
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.