惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
V
Visual Studio Blog
The Register - Security
The Register - Security
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
L
LangChain Blog
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
T
Threatpost
P
Proofpoint News Feed
美团技术团队
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
Vercel News
Vercel News
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
S
Security @ Cisco Blogs
AWS News Blog
AWS News Blog
SecWiki News
SecWiki News
I
InfoQ
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
B
Blog RSS Feed
H
Hacker News: Front Page
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2021-02-06 · via 轶哥博客

如果我想定义json的内容(root)既可以是一个对象,又可以是一个数组,应该如何书写json schema?

这个问题换一种描述方式,“json schema定义了一个对象,但是希望内容也可以是这个对象的数组,如何书写json schema?”。

例如,Typescript中:root: object | [object]

例如,我希望数据可以是下面这样的一个对象:

{
  "method": "GET"
}

也可以是这样的数组:

[
  {
    "method": "GET"
  }
]

根据https://json-schema.org/公开的Json Schema草案,可以这样来实现:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": [
    "array",
    "object"
  ],
  "items": {
    "$ref": "#"
  },
  "properties": {
    "method": {
      "type": "string",
      "enum": [
        "GET",
        "POST",
        "PUT",
        "DELETE",
        "OPTIONS",
        "PATCH",
        "HEAD"
      ]
    }
  }
}

这样的写法就可以兼容以上两种情况了。其中$ref的值为#,代表引用自身。

如果希望method即可以是一个string类型的枚举类型,也可以是包含上述定义的数组。可以这样编写:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": [
    "array",
    "object"
  ],
  "items": {
    "$ref": "#"
  },
  "definitions": {
    "method": {
      "type": "string",
      "title": "请求方法",
      "description": "支持的 HTTP 请求方法。目前支持 'DELETE'、'GET'、'HEAD'、'PATCH'、'POST'、'PUT' 以及 'OPTIONS'。它还可以是一个 HTTP 方法的数组。",
      "default": "GET",
      "enum": [
        "GET",
        "POST",
        "PUT",
        "DELETE",
        "OPTIONS",
        "PATCH",
        "HEAD"
      ]
    }
  },
  "properties": {
    "method": {
      "anyOf": [
        {
          "$ref": "#/definitions/method"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/definitions/method"
          }
        }
      ]
    }
  }
}

这样一来,自动补全提示如下:

jsonschema.png

因此,

[
  {
    "method": [
      "GET",
      "PUT"
    ]
  }
]

[
  {
    "method": "POST"
  }
]

都是合法的。