JSON Schema Validator

One input per line. Output is numbered to match the input order.
Pinned tools are listed in your favourites on the home page.Copies a link to this tool that carries your current input, so it opens ready to run.Gives you an iframe snippet for putting this tool on your own site.
JSON Schema
JSON Data
Ctrl+Enter Run  · Ctrl+Shift+C Copy  · Esc Clear

Understand JSON Schema Validator

这个校验器用 JSON Schema 检查 JSON 文档,并逐条列出数据不匹配之处,同时给出问题值所在的路径。

How it works

它把数据和 schema 一起遍历,在每一层应用各关键字的约束:type(含 integer)、enum、required、properties、additionalProperties、pattern、minLength 与 maxLength、minimum 与 maximum、items、minItems 与 maxItems。错误会带上像 $.user.email 这样的路径,好让你在大型文档中找到那个值。当某个值类型不符时,遍历就在那里停下,因为对它的任何后续检查都只会是噪声。

When to use it

  • 检查 API 响应是否仍与你的客户端所期望的结构一致。
  • 用一份应当通过和一份应当失败的样本,测试你正在编写的 schema。
  • 在大型载荷中找出某个服务拒绝的是哪个字段。
  • 确认配置文件符合其所属工具自己发布的 schema。

Watch out for

  • JSON Schema 默认是宽松的。除非 additionalProperties 为 false,否则未在 properties 中列出的属性也是允许的;而已列出的属性除非出现在 required 中,否则都是可选的——看上去严格的 schema 通常并不严格。
  • 这里实现的是 draft-07 的一个子集。$ref、allOf、anyOf、oneOf、not、const 和 format 都不会被求值,因此使用了它们的 schema 只会就能检查的部分报告,对其余部分保持沉默。
  • type: number 接受整数,而 type: integer 既接受 1 也接受 1.0,因为 JSON 只有一种数字类型,1.0 就是那个数。
  • 类型不符会压制该值上的其余错误,因此修好一个错误可能会让底下更多错误浮现出来。

Not the right tool for: 依赖 $ref、组合器或 format 断言的 schema。Ajv 是参考实现,它能检查本工具做不到的部分。

Frequently Asked Questions

支持哪些 JSON Schema 关键字?

draft-07 的核心子集:type(含 integer 和类型数组)、required、properties、additionalProperties、enum、pattern、minLength/maxLength、minimum/maximum、items、minItems/maxItems,以及嵌套 schema。未实现:format、const、$ref,以及 allOf/anyOf/oneOf/not 组合器。若需要完整的 draft-07 覆盖,请在自己的构建中使用 Ajv。

JSON Schema draft-07 和 2020-12 有什么区别?

draft-07 是部署最广泛的版本(许多工具默认采用)。draft 2020-12 增加了 $dynamicRef、unevaluatedProperties,并改进了 $ref 行为。除非你的工具链明确要求更新的草案,否则请使用 draft-07。

如何用 schema 校验 API 响应?

把 API 响应粘进「数据」字段,把你的 OpenAPI/Swagger schema 组件粘进「Schema」字段。这有助于在编写客户端代码之前,确保 API 响应与其文档中的结构一致。

How to Use JSON Schema Validator

  1. Paste or type your input in the input area above.
  2. The tool processes your input automatically or click Run.
  3. Copy or download the result using the action buttons.
  4. Use Ctrl+Enter to run quickly from the keyboard.