fix(annotator): validate CourtListener result elements before use (#237)#244
Merged
Merged
Conversation
isSearchResponse only checked Array.isArray(results); element fields were never validated and cast straight to CourtListenerResult. A result missing or mistyping a field (no snippet, non-array citation, numeric court) made annotateSection throw on result.citation[0] / mapCourt(...) / truncation — escaping its Result<> contract as a rejected promise that a caller checking result.ok never sees, and that a single bad element could use to abort a batch. Add isCourtListenerResult to type-check every field and have searchByStatute drop malformed elements (logging the count), so callers only ever receive well-formed results. Replaces the envelope-only check with hasResultsArray. Adds client.test.ts covering the validator and the drop-malformed behaviour (real client + stubbed fetch). Closes #237 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
isSearchResponseonly checkedArray.isArray(results)— individual elements were cast straight toCourtListenerResultwithout field validation. The CourtListener search API is untrusted, so a result element missing or mistyping a field (nosnippet, a non-arraycitation, a numericcourt) madeannotateSectionthrow onresult.citation[0]/mapCourt(...)/ truncation. BecauseannotateSectionreturns aResult<>, that throw escaped as a rejected promise — invisible to a caller checkingresult.ok, and able to abort a whole annotation batch on one bad element.Fix
isCourtListenerResultthat type-checks every field (caseName,citation: string[],court,dateFiled,snippet,absolute_url).searchByStatutenow drops malformed elements (logging the dropped count) and returns only well-formed results, so callers never see a partial object. The envelope-only check is renamedhasResultsArray.Graceful degradation: a malformed element is skipped rather than tanking the section.
Tests
client.test.ts(new): validator unit tests (missing/mistyped fields rejected) +searchByStatutefiltering malformed elements via a real client with stubbedfetch.Annotator package: lint, typecheck, and 110 tests green locally.
Closes #237