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}$"
    }
  }
}
相关推荐
石像鬼₧魂石1 小时前
内网渗透靶场实操清单(基于 Vulhub+Metasploitable 2)
linux·windows·学习·ubuntu
flying robot2 小时前
centos7系统配置
笔记
醇氧3 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
敲敲了个代码4 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
Pandora_4174 小时前
cursor学习
学习
zhdy567895 小时前
最简单方法 设置matlab坐标轴刻度标签的字号,设置坐标轴标题和图形标题,并指定字号。画出的图片背景设置为白色,
笔记
崇山峻岭之间5 小时前
Matlab学习笔记02
笔记·学习·matlab
木木em哈哈5 小时前
C语言多线程
笔记
Kiri霧6 小时前
Range循环和切片
前端·后端·学习·golang
hssfscv7 小时前
Javaweb 学习笔记——html+css
前端·笔记·学习