validate-a-response-body-against-its-schema
Use when parsing third-party JSON. Validate the payload against a schema before use so a silent field change fails loudly at the boundary, not deep in business logic.
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 81
- Forks
- 17
01 前言 Front Matter
- name
- validate-a-response-body-against-its-schema
- description
- Use when parsing third-party JSON. Validate the payload against a schema before use so a silent field change fails loudly at the boundary, not deep in business logic.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md369 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Validate a response body against its schema
Parsing a third-party payload on trust turns a removed field into a KeyError three layers down
or, worse, a silent None that flows into a sum. Check the shape at the boundary.
Procedure
- Derive a schema from the OpenAPI spec or, absent one, from observed responses, and commit it as
schemas/item.schema.json. - Validate before touching any field:
import jsonschema try: jsonschema.validate(instance=r.json(), schema=SCHEMA) except jsonschema.ValidationError as e: raise ValueError(f"schema mismatch at {list(e.absolute_path)}: {e.message}") - Set
"required": [...]explicitly to the fields you depend on rather than trustingadditionalProperties. - Type-check numbers: a schema
"type": "number"catches the server that stringifies an id. - Validate once at the boundary in a
parse_*function per endpoint, then pass typed objects inward. - For huge payloads, validate the structural keys plus a sampled subset to keep cost bounded.
- Fail closed in production (raise/alert); log-and-continue only where the run genuinely survives without the field.
Pitfalls
additionalProperties: falsebreaks on additive server changes; set it true and require what you need.- Validating only page one misses variant shapes on later pages.
NaN/Infinitypass some parsers but fail strict schema numbers; normalise before validating.- A schema copied once drifts from the API and becomes fiction; regenerate from the spec.
{}validates against a schema with norequiredfields, hiding a total failure.
Verification
python -m jsonschema -i fixture.json schemas/item.schema.json && echo OK
The fixture passes and a deliberately corrupted fixture fails with a named path. Report: "Validated 10,000 responses; 3 rejected with field paths; 0 reached business logic malformed."
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.