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.

更新 2026-10-31 · 公式可核验

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.

Sources

Related calculators

JSON to TypeScriptPaste JSON to generate interfaces/types; array objects merge fields and missing keys become optional, all locally.

More guides

JSON to TypeScript: interface or typeRead guide