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}$"
    }
  }
}
相关推荐
智者知已应修善业4 小时前
【51单片机普通延时奇偶灯切换】2023-4-4
c语言·经验分享·笔记·嵌入式硬件·51单片机
wdfk_prog4 小时前
[Linux]学习笔记系列 -- [block]bio
linux·笔记·学习
9084869055 小时前
文旅业务相关前沿技术应用
学习·产品经理
GIS学姐嘉欣5 小时前
地信、测绘、遥感等专业免费学习网站推荐
学习·gis开发·webgis
卡提西亚7 小时前
C++笔记-34-map/multimap容器
开发语言·c++·笔记
今天你TLE了吗8 小时前
Stream流学习总结
java·学习
一个平凡而乐于分享的小比特9 小时前
UCOSIII笔记(十三)CPU利用率及栈检测统计与同时等待多个内核对象
笔记·ucosiii
摇滚侠10 小时前
2025最新 SpringCloud 教程,编写微服务 API,笔记08
笔记·spring cloud·微服务
周全全11 小时前
基于ElasticSearch的语义检索学习-向量化数据、向量化相似度、向量化检索
大数据·学习·elasticsearch
4***721311 小时前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
爬虫·学习·计算机外设