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}$"
    }
  }
}
相关推荐
qq_386322693 小时前
华为网路设备学习-32(BGP协议 七)路由反射器与联邦
网络·学习
萘柰奈3 小时前
Unity学习----【进阶】Addressables(二)--加载资源与打包及更新
学习·unity
liliangcsdn7 小时前
Leiden社区发现算法的学习和示例
学习·数据分析·知识图谱
程序员Xu7 小时前
【LeetCode热题100道笔记】二叉树的右视图
笔记·算法·leetcode
程序员Xu8 小时前
【LeetCode热题100道笔记】二叉搜索树中第 K 小的元素
笔记·算法·leetcode
DKPT8 小时前
JVM中如何调优新生代和老生代?
java·jvm·笔记·学习·spring
phltxy8 小时前
JVM——Java虚拟机学习
java·jvm·学习
我真的是大笨蛋11 小时前
K8S-基础架构
笔记·云原生·容器·kubernetes
m0_5713728211 小时前
嵌入式学习——ARM 体系架构1
arm开发·学习
Rhys..11 小时前
python + Flask模块学习 2 接收用户请求并返回json数据
python·学习·flask