Add runtime validation for collection config with helpful error messages#1278
Open
KyleAMathews wants to merge 3 commits intomainfrom
Open
Add runtime validation for collection config with helpful error messages#1278KyleAMathews wants to merge 3 commits intomainfrom
KyleAMathews wants to merge 3 commits intomainfrom
Conversation
…ble error messages TypeScript errors from createCollection's 8 overloads with deeply nested generics are extremely hard to read. This adds comprehensive runtime validation that catches common misconfigurations with clear, actionable error messages including examples and typo suggestions. Validations added: - Missing or wrong-type getKey (must be a function) - Missing or wrong-type sync config (must be object with sync function) - Wrong-type callback options (onInsert, onUpdate, onDelete, compare) - Wrong-type scalar options (id, gcTime, startSync, autoIndex, syncMode, utils) - Unknown config properties with Levenshtein-based "did you mean?" suggestions Also fixes: - localStorageCollectionOptions leaking `parser` property into CollectionConfig - Test utility mockSyncCollectionOptions leaking `initialData` into CollectionConfig https://claude.ai/code/session_01SoqPVFMvsgwf2ciuSU1wRF
Wrap validateCollectionConfig() in a process.env.NODE_ENV check so bundlers can tree-shake the validation code (including Levenshtein distance, error classes, etc.) in production builds. https://claude.ai/code/session_01SoqPVFMvsgwf2ciuSU1wRF
|
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: +1.86 kB (+2.02%) Total Size: 93.9 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.7 kB ℹ️ View Unchanged
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🎯 Changes
This PR adds comprehensive runtime validation for the
createCollectionconfiguration to catch common misconfiguration mistakes early with clear, actionable error messages.Key additions:
New validation module (
validate-config.ts): Validates collection config at runtime before passing toCollectionImpl, checking:getKeyfunction is present and is a functionsyncconfig is present, is an object, and contains asyncfunctiononInsert,onUpdate,onDelete,compare) are functions if providedidas string,gcTimeas number,startSyncas boolean, etc.)autoIndex: "off"/"eager",syncMode: "eager"/"on-demand")New error classes in
errors.ts:CollectionRequiresGetKeyErrorInvalidGetKeyErrorInvalidSyncConfigErrorInvalidSyncFunctionErrorInvalidCallbackOptionErrorInvalidOptionTypeErrorUnknownCollectionConfigError(with typo suggestions and valid property list)Integration:
createCollectionnow callsvalidateCollectionConfigbefore creating the collection instanceComprehensive test suite (
collection-config-validation.test.ts): 400+ lines of tests covering all validation scenarios, error messages, typo detection, and valid configurationsBenefits:
✅ Checklist
pnpm test.🚀 Release Impact
Note: This is a dev-time improvement that adds validation to catch configuration errors earlier. The validation runs at collection creation time but doesn't change the public API or behavior of correctly-configured collections.
https://claude.ai/code/session_01SoqPVFMvsgwf2ciuSU1wRF