GUIDE深度 · Deep dive
API JSON Validation: Types, Optional Fields and Versions
Adding fields is usually compatible; removing fields/changing types breaks clients. After generating types, use Zod/Valibot for runtime validation and test optional fields/versioned responses; do not treat sample JSON as a contract.
Validation strategy
Validate external JSON at the boundary.
Default optional fields.
Decide whether to ignore/reject unknown keys.
Versioning
Add versioning/contract tests.
Deprecate before removing fields.
FAQ
Do TS types guarantee the API?
No; runtime validation does.
Should extra fields error?
Depends on compatibility; strict rejects, lenient ignores.