Json Schema 学习笔记

一、基本结构

复制代码
{
  "$schema": "http://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age":  { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" },
    "isActive": { "type": "boolean", "default": true }
  },
  "required": ["name", "email"]
}

二、常用关键词说明

|---------------------------|-------------------------------------------------------------|
| 关键词 | 说明 |
| type | 数据类型(string, number, integer, boolean, object, array, null) |
| properties | 对象中每个字段的定义 |
| required | 指定哪些字段是必须的 |
| minimum / maximum | 用于 number/integer 的值约束 |
| minLength / maxLength | 字符串长度约束 |
| enum | 指定取值范围 |
| pattern | 正则表达式校验字符串 |
| format | 格式校验(如 date-time、email、uri) |
| default | 默认值(通常用于文档或生成工具) |

三、嵌套结构示例

复制代码
{
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "username": { "type": "string" },
        "roles": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "required": ["username"]
    }
  }
}

四、数组类型写法

复制代码
{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "value": { "type": "string" }
    },
    "required": ["id"]
  }
}

五、枚举和正则校验

复制代码
{
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": ["draft", "published", "archived"]
    },
    "phone": {
      "type": "string",
      "pattern": "^1[3-9]\\d{9}$"
    }
  }
}
相关推荐
Bechamz5 分钟前
大数据开发学习Day37
大数据·学习
zxd0203115 分钟前
Zabbix7 监控系统学习总结
学习
z200509307 分钟前
【linux学习】在linux下使用git提交到gitee
git·学习·gitee
叶~小兮7 分钟前
Zabbix 7.0学习笔记
笔记·学习·zabbix
一条泥憨鱼9 分钟前
【Java 进阶】LinkedHashMap 与 TreeMap
java·开发语言·数据结构·笔记·后端·学习
ゆづき9 分钟前
假如编程语言们有外号
java·c语言·c++·python·学习·c#·生活
xuhaoyu_cpp_java10 分钟前
Linux学习(一)
linux·经验分享·笔记·学习
red_redemption13 分钟前
自由学习记录(189)
学习
yyyyy_abc9 小时前
ceph学习笔记
笔记·ceph·学习
晓梦林9 小时前
ximai靶场学习笔记
android·笔记·学习