fix: improve TypeScript type generation for schemas with anyOf patterns#91
Merged
Conversation
## Problem
Schemas using `anyOf` with inline object properties prevent TypeScript
code generators from creating useful type definitions, producing
`{ [k: string]: unknown }` instead of proper union types.
## Changes
### Package Definition (create_media_buy)
- Extracted package definition to separate `package-request.json` schema
- Changed from `anyOf` to `oneOf` for mutual exclusivity
- Added `not` clauses to enforce format_ids XOR format_selection
- Updated `create-media-buy-request.json` to use $ref
### Image Asset Dimensions (standard-formats)
- Refactored `image.json` from inline properties + anyOf to proper oneOf
- Two variants: exact dimensions (width/height) OR flexible (acceptable_dimensions)
- Added mutual exclusivity constraints
### Schema Registry
- Added `package-request` to media-buy supporting-schemas section
## Impact
TypeScript generators will now produce proper discriminated union types:
```typescript
type Package =
| { buyer_ref: string; products: string[]; format_ids: string[]; ... }
| { buyer_ref: string; products: string[]; format_selection: object; ... };
```
## Testing
✅ All schema validation tests passing
✅ All cross-references resolve correctly
✅ Build successful
✅ Backward compatible - JSON validation rules unchanged
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
5 tasks
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.
Problem
Schemas using
anyOfwith inline object properties prevent TypeScript code generators from creating useful type definitions. This affects all TypeScript implementations of AdCP clients, producing{ [k: string]: unknown }instead of proper union types.Root Cause
When
anyOfis used only to vary therequiredfields while all properties are defined inline above theanyOf, TypeScript generators see conflicting type constraints and fall back to genericunknowntypes.Changes
1. Package Definition (create_media_buy)
static/schemas/v1/media-buy/package-request.json- extracted package definition to separate schema fileanyOftooneOffor better semantic correctness (mutual exclusivity)notclauses to enforceformat_idsXORformat_selectioncreate-media-buy-request.jsonto use$refto package-request.json2. Image Asset Dimensions (standard-formats)
image.jsonfrom inline properties +anyOfto properoneOfstructurewidth/height) OR flexible dimensions (acceptable_dimensions)notclauses3. Schema Registry
package-requesttomedia-buy.supporting-schemassection inindex.jsonImpact on TypeScript Generation
Before:
After:
This provides:
Safe Patterns Identified
During the audit, I found these schemas use
anyOfbut are safe for TypeScript generation:manage-creative-library-request.json- UsesallOfwith conditionals (proper pattern)creative-asset.json- UsesoneOffor discriminated union (proper pattern)Breaking Change Assessment
Non-breaking: This is a refactoring that maintains identical JSON validation behavior. The validation rules are unchanged - only the schema structure is improved for better code generation.
Testing
✅ All schema validation tests passing (6/6)
✅ All cross-references resolve correctly
✅ Schema registry consistency verified
✅ All example validation tests passing (7/7)
✅ Build successful
✅ TypeScript compilation successful
Related
This addresses the schema issue documented in the issue draft and improves developer experience for all TypeScript implementations of AdCP.
🤖 Generated with Claude Code