Add Orval + Zod runtime API response validation - #1414
Closed
hidden4003 wants to merge 2 commits into
Closed
Conversation
Hand-written API response types can silently drift from what Shoko Server actually returns, letting malformed-but-valid-JSON responses reach components as undefined fields in unrelated places. Orval now generates types and Zod schemas from the server's live OpenAPI spec into src/core/api/generated/ (committed, regenerate via `pnpm orval`), and a validateResponse() helper throws a SchemaValidationError on mismatch, which queryClient.ts surfaces as an immediate toast instead of retrying 4x. tag and series-tags react-query hooks are converted as a pilot; the rest of src/core/react-query/ can follow the same pattern incrementally. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
|
So right now we only generate types and not the hooks also? |
Chaining .then(data => validateResponse(schema, data, context)) at every call site was easy to forget and added boilerplate. Schemas now travel as a `schema` field on the request config instead; the axios/axiosV2/axiosPlex response interceptor validates automatically when present, and the resolved type is inferred from the schema with no explicit generic needed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
Author
Our hooks have custom logic in them which is why I did not set up hook generation as well, I can add it if you want to see how it looks. |
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
src/core/api/generated/(committed; regenerate locally viapnpm orvalagainst a running server, since CI has none).validateResponse()/SchemaValidationError(src/core/api/validateResponse.ts) so a response that doesn't match its schema throws instead of letting malformed data silently reach components asundefinedfields.queryClient.tssurfaces aSchemaValidationErroras an immediate "Invalid API Response" toast, skipping the existing 4x retry (retrying won't fix a shape mismatch).tagand the tag-relatedserieshooks to prove the pattern; the rest ofsrc/core/react-query/is intentionally left for incremental follow-up.TagType.Sourcewas typed as a closed'AniDB' | 'User'union when the server doesn't actually constrain it, and acleanString()helper only guarded againstundefined, notnull(which the real schema allows forDescription).Draft because this establishes a new pattern (Orval + Zod validation, a
src/core/api/generateddirectory excluded from oxlint, a few workarounds for upstream Orval/spec quirks) that I'd like other maintainers to weigh in on before it's adopted more broadly.Test plan
pnpm dprint,pnpm oxlint,pnpm stylelint,pnpm tscheckall passpnpm orvalruns cleanly against a live Shoko Server dev instancepnpm startboots and proxies correctly🤖 Generated with Claude Code