如何第一时间看到新文章?
收藏本博客列表页,并在首页与工具聚合页留意指南入口。阅读文章无需注册或邮件订阅。
可复制 JSON Schema 示例:对象、字符串、数组、enum、anyOf、$ref 等,并梳理常见校验误区,便于配合 JSON Work 本地校验与转 Schema 工具使用。
JSON Schema 用来描述 JSON 数据的允许结构与规则。语法校验回答「是不是合法 JSON」;Schema 校验回答「是否符合我们的数据契约」。
下文示例对齐 Draft 2020-12(见 $schema)。请与你使用的校验器支持的草案版本保持一致。
若需复习 JSON 基础,可先读 什么是 JSON?。
name(字符串)与 age(整数),tags 可选:{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/person.min.json",
"title": "Person",
"type": "object",
"additionalProperties": false,
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "age"]
}合法实例{ "name": "Asha", "age": 30, "tags": ["beta"] }不合法实例(缺少 age,且 additionalProperties: false 禁止未知字段 extra){ "name": "Asha", "extra": true }{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"handle": { "type": "string", "pattern": "^[a-z0-9_]{3,20}$" },
"contact": { "type": "string", "format": "email" },
"createdAt": { "type": "string", "format": "date-time" }
},
"required": ["handle"]
}提示: 在核心规范里 format 常作为注解;Ajv 等需插件才会按格式校验。未开启时把 format 当作文档约定更安全。{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"qty": { "type": "integer", "minimum": 1, "maximum": 99 },
"price": { "type": "number", "minimum": 0, "multipleOf": 0.01 }
},
"required": ["qty", "price"]
}整数用 integer,需要小数时用 number。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"role": { "type": "string", "enum": ["admin", "editor", "viewer"] },
"version": { "const": 1 }
},
"required": ["role", "version"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"ids": {
"type": "array",
"items": { "type": "string", "pattern": "^[A-Z]{3}-[0-9]{4}$" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
},
"required": ["ids"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"value": {
"anyOf": [
{ "type": "string", "minLength": 1 },
{ "type": "number" }
]
}
},
"required": ["value"]
}oneOf — 恰好一个分支(注意分支条件不要重叠)。allOf — 合并所有子 schema 的约束。{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"Email": { "type": "string", "format": "email" }
},
"type": "object",
"additionalProperties": false,
"properties": {
"owner": { "$ref": "#/$defs/Email" },
"billing": { "$ref": "#/$defs/Email" }
},
"required": ["owner"]
}在根对象上使用 additionalProperties: false 有助于捕获字段拼写错误。对外 API 通常与明确的 properties、required 一起使用。
{ "type": ["string", "null"] } 等写法。生产环境常用 Ajv(JavaScript)或 jsonschema(Python)等;务必核对库对所声明 $schema 草案的支持情况。
致力于为开发者提供最佳的 JSON 处理工具
更多文章即将发布...
返回博客关于跟进更新、选题方向与互动反馈。
收藏本博客列表页,并在首页与工具聚合页留意指南入口。阅读文章无需注册或邮件订阅。
围绕 JSON 校验、格式化、转换与调试流程,以及 JSON Work 工具更新,与在线工具的本地能力一一对应。
可以。请通过关于页的联系方式或 GitHub 反馈;我们会优先安排贴近真实开发场景的教程。