美文网首页
json schema step by step 2022-10

json schema step by step 2022-10

作者: 9_SooHyun | 来源:发表于2022-10-25 15:14 被阅读0次

本文对json schema做基本的介绍
更多语法可访问官网,有详细的例子:http://json-schema.org/understanding-json-schema/

step1 $schema & type

# schema0
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "string"
}

The $schema keyword is used to declare which dialect of JSON Schema the current schema was written for. The value of the $schema keyword is also [the identifier for a schema that can be used to verify that the current schema is valid]. A schema that describes another schema is called a “meta-schema”.
$schema的值是另一个schema的id,表示使用目标元schema检查当前编写的schema是否valid

type keyword is used to specify the value's type. basic types are the following:

string number integer object array boolean null

In the following example, we accept strings and numbers, but not structured data types:
{ "type": ["number", "string"] }
而每一种type下又有各自支持的其他keyword

# 如`{ "type": "string" }`还支持`minLength``maxLength`和`regex`对string做进一步限制
{
  "type": "string",
  "minLength": 2,
  "maxLength": 3,
  "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
}

# 0 <= value < 100
{
  "type": "number",
  "minimum": 0,
  "exclusiveMaximum": 100
}

so, "i am a string" fits schema0, but ["hhh"] not

step2 enum & const

The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.

we can use enum even without a type, to accept values of different types. Let’s extend the example to use null to indicate “off”, and also add 42, just for fun.

{
  "enum": ["red", "amber", "green", null, 42]
}

The const keyword is used to restrict a value to a single value.

# schema1
{"const": "United States of America"}

only string "United States of America" fits schema1

step3 Schema Composition

allOf: (AND) Must be valid against all of the subschemas
anyOf: (OR) Must be valid against any of the subschemas
oneOf: (XOR) Must be valid against exactly one of the subschemas
http://json-schema.org/understanding-json-schema/reference/combining.html

tools:
create related jsonschema according to json
json schema validator

相关文章

  • json schema step by step 2022-10

    本文对json schema做基本的介绍更多语法可访问官网,有详细的例子:http://json-schema.o...

  • eslint & lint-stage & husky搭建自动构

    Step 1: 安装包 Step 2: 配置.eslintrc.js Step 3: 配置package.json...

  • Moco启动

    step1:下载moco-runner step2:配置Json文件 step3:在 Terminal终端进入mo...

  • Workbench :导出SQL

    step1:选择顶栏的Server > Data Export step2:选择要导出的Schema 修改文件名和路径

  • flutter json

    第三方工具 json to dart step1: 引包 step2:

  • 2018-11-08 Reptile POST

    import requests import json #The third step of a reptile ...

  • step by step

    忙毕业的事,一直没有时间和心情去研究彩铅画,难的这几天有空闲时间,就找了画纸,买了彩铅(超市里买的儿童24色彩铅笔...

  • Step by step

    我相信,每一个光鲜亮丽的外表下,都有一段执拗而又孤独的坚持。 如果人生只有一次翻身的机会,那么你一定要拼尽全力。 ...

  • Step by step

    今天是第一天来到简书,关于写作这件事情,始终是蛰伏在心底的。 这个星期四,我在数学课上,忽然想换个微博,想在微博上...

  • step by step

    今天开始,慢慢来。 一切,也都会慢慢的到来❤️

网友评论

      本文标题:json schema step by step 2022-10

      本文链接:https://www.haomeiwen.com/subject/anwazrtx.html