Skip to content

feat: integrate Zod schemas for opt-in runtime validation#126

Open
benminer wants to merge 2 commits intomainfrom
feat/schema-integration
Open

feat: integrate Zod schemas for opt-in runtime validation#126
benminer wants to merge 2 commits intomainfrom
feat/schema-integration

Conversation

@benminer
Copy link
Collaborator

Wires the auto-generated Zod schemas (from #124) into the SDK's resource layer for opt-in runtime validation.

Usage

const client = new Scope3Client({
  apiKey: 'sk_xxx',
  persona: 'buyer',
  validate: true,       // validate both inputs and responses
  // validate: 'input'  // validate request bodies only
  // validate: 'response' // validate API responses only
});

When enabled, invalid inputs throw Scope3ApiError(400) and unexpected API response shapes throw Scope3ApiError(502) with structured Zod validation details.

Default is off — zero overhead for existing consumers.

Changes

  • src/validation.ts — Thin validation utility (validateInput, validateResponse, mode helpers)
  • src/schemas/registry.ts — Hand-maintained mapping from resource operations to auto-generated Zod schemas
  • src/types/index.ts — Add validate option to Scope3ClientConfig
  • src/adapters/base.ts — Add validate to BaseAdapter interface
  • src/adapters/rest.ts, src/adapters/mcp.ts — Thread validate config through adapters
  • Resource files — Wire validation into campaigns, reporting, sales-agents, bundles, and products
  • Tests — 14 new validation tests, all 239 tests pass

Design Decisions

  • Validate at the resource layer, not the adapter — each method knows its schemas
  • Keep hand-written types as the public API surface; schemas are runtime guards only
  • Schema registry bridges auto-generated schemas to SDK methods without modifying generated code
  • Response validation applied broadly; input validation only where SDK types align with OpenAPI schemas (some have diverged, e.g., advertiser brandDomain vs API brand)

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 <[email protected]>
@benminer benminer requested a review from a team March 20, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant