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}$"
    }
  }
}
相关推荐
c76916 分钟前
【文献笔记】ARS: Automatic Routing Solver with Large Language Models
人工智能·笔记·语言模型·自然语言处理·llm·论文笔记·cvrp
cherishSpring1 小时前
Redis学习笔记
redis·笔记·学习
★YUI★1 小时前
学习游戏制作记录(战斗系统简述以及击中效果)7.22
学习·游戏
AI扶我青云志1 小时前
bert模型中config.json中所有参数
人工智能·json·bert
喜欢你,还有大家3 小时前
Linux笔记2——常用命令-1
linux·服务器·笔记
Ro Jace3 小时前
图像分析学习笔记(2):图像处理基础
图像处理·笔记·学习
慕y2743 小时前
Java学习第六十三部分——K8s
java·开发语言·学习
我爱学嵌入式5 小时前
C语言第 4 天学习笔记:位运算、流程控制与输入输出
linux·c语言·笔记
Star在努力5 小时前
C语言:第11天笔记
c语言·开发语言·笔记
kyle~6 小时前
数据交换---JSON格式
服务器·microsoft·json