feat: integrate Zod schemas for opt-in runtime validation#126
Merged
Conversation
Add opt-in runtime validation using auto-generated Zod schemas from the OpenAPI spec. Validation is controlled via a new 'validate' option on Scope3ClientConfig: - false/undefined (default): no validation, zero overhead - true: validate both inputs and responses - 'input': validate request bodies only - 'response': validate API responses only Changes: - Add ValidateMode type and validation utilities (src/validation.ts) - Add schema registry mapping operations to Zod schemas (src/schemas/registry.ts) - Thread validate config through BaseAdapter interface to resources - Wire response validation into campaigns, reporting, sales-agents, bundles, and products resources - Wire input validation into sales-agents, bundles, and products - Add validation unit tests (14 new tests) - Export validateInput/validateResponse for consumer use Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Change validate config default from undefined (off) to true. The schemas are generated from the OpenAPI spec and represent the correct API contract - validation should be on by default. Consumers can opt out with validate: false.
EmmaLouise2018
approved these changes
Mar 23, 2026
- Use return value from validateInput/validateResponse instead of discarding it, so Zod defaults and transforms are applied - Remove validation from bundles resource: schemas have diverged significantly (advertiserId: number vs string, discoveryId vs bundleId) - Remove input validation from bundles.create() which used the wrong schema (DiscoverProductsBody instead of a bundle creation schema) - Keep reporting validateResponse as check-only (generic T prevents reassignment)
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.
Wires the auto-generated Zod schemas (from #124) into the SDK's resource layer for runtime validation.
Usage
Validation is enabled by default. No config changes needed — all SDK methods that have matching schemas will validate inputs and responses automatically.
When enabled, invalid inputs throw
Scope3ApiError(400)and unexpected API response shapes throwScope3ApiError(502)with structured Zod validation details.Changes
src/validation.ts— Validation utility (validateInput,validateResponse, mode helpers)src/schemas/registry.ts— Hand-maintained mapping from resource operations to auto-generated Zod schemassrc/types/index.ts— Addvalidateoption toScope3ClientConfigsrc/adapters/base.ts— AddvalidatetoBaseAdapterinterfacesrc/adapters/rest.ts,src/adapters/mcp.ts— Threadvalidateconfig through adapters (default:true)Design Decisions
validateInput/validateResponseare sent to the API / returned to consumers, so Zod defaults and transforms are appliedadvertiserId: numbervsstring,discoveryIdvsbundleId)