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}$"
    }
  }
}
相关推荐
酿情师28 分钟前
yihan:一款面向连续网页学习的智能侧边栏插件
学习·学习方法·工具·学习工具
九转成圣44 分钟前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
瞎某某Blinder1 小时前
DFT学习记录[6]基于 HES06的能带计算+有效质量计算
python·学习·程序人生·数据挖掘·云计算·学习方法
love在水一方2 小时前
VLN 入门学习计划 —— 基于 InternNav
学习
糖炒栗子03263 小时前
【笔记】高分卫星影像 TIF 切片处理
笔记
Nice_Fold3 小时前
Kubernetes DaemonSet、StatefulSet与Service(自用笔记)
笔记·容器·kubernetes
小袁拒绝摆烂4 小时前
多表关联大平层转JSON树形结构
java·json
red_redemption5 小时前
自由学习记录(175)
学习
ZhiqianXia6 小时前
《The Design of Design》阅读笔记
前端·笔记·microsoft
nashane6 小时前
HarmonyOS 6学习:画中画(PiP)状态同步与场景化实战指南
学习·pip·harmonyos·harmonyos 5