JSON Schema

JSON Schema Validation: A Vocabulary for Structural Validation of JSON

キーワード

Keyword用途Value型Schema Sample
すべて

type 型定義 string or array

"type": "object"

"type": "string"

"type": "number"

"type": ["string", "number"]

enum 列挙定義 array

"enum": ["東京", "大阪", "名古屋"]

const 定数定義 すべて

"const": 5

"const": "日本"

title タイトル string "title": "sample api schema"
description 概要 string "description": "sample api schema"
default デフォルト すべて

"default" : "default value"

readOnly 読出しのみ boolean "readOnly": true
writeOnly 書込みのみ boolean "writeOnly": true
examples サンプル array "samples": ["abc", "def", "ghi"]
数字
number & integer


multipleOf 倍数 number

"multipleOf": 5

"multipleOf": 2.5

maximum 最大値 <= number "maximum": 100
exclusiveMaximum 最大値 < number "exclusiveMaximum": 100
minimum 最小値 >= number "minimum": 100
exclusiveMinimum 最小値 > number "exclusiveMinimum": 100
String

maxLength 最長 non-negative integer "maxLength": 64
minLength 最短 non-negative integer "minLength": 4
pattern 正規表現よる文字列定義 string "pattern": "[abc]+"
Array




items 配列要素のスキーマ定義 JSON schema
or array of JSON schema
"items": { "type": "integer" }
additionalItems 拡張要素のスキーマ定義 JSON schema "additionalItems": { "type": "string" }
maxItems 最大サイズ non-negative integer "maxItems": 10
minItems 最小サイズ non-negative integer "minItems": 10
uniqueItems 同じ値の要素が許可できるか boolean "uniqueItems": true
contains 必須要素タイプ JSON schema "contains": { "type": "integer" }
Object






maxProperties 属性数最大値 non-negative integer "maxProperties": 5
minProperties 属性数最小値 non-negative integer "minProperties": 2
required 必須属性 array

"required": ["name", "email"]

properties 属性定義 object

"properties": {
"name": { "type": "string" },
"id": { "type": "number","minimum": 2 }
"enum": ["man", "woman", "others"]
}

patternProperties 正規表現よる属性定義 object

"patternProperties": {
"^class.*$": { "type": "string" },
"^grade.*$": { "type": "number" }
}

additionalProperties 拡張属性のスキーマ定義 JSON schema "additionalProperties": { "type": "string" }
dependencies 依存関係 object

"dependencies": {
"credit_card": {
"properties": "billing_address": { "type": "string" }
},
"required": ["billing_address"]
}
}

propertyNames 属性名 JSON schema "propertyNames": { "format": "email" }
条件キーワード

if/then/else 条件判定よるスキーマ定義 JSON Schema

{
"if": { "properties": { "power": { "minimum": 9000 } } },
"then": { "required": [ "disbelief" ] },
"else": { "required": [ "confidence" ] }
}

{
"type": "integer",
"minimum": 1,
"maximum": 1000,
"if": { "minimum": 100 },
"then": { "multipleOf": 100 },
"else": {
"if": { "minimum": 10 },
"then": { "multipleOf": 10 }
}
}

allOf 複数条件 array

"allOf": [
{ "maximum": 3 },
{ "type": "integer" }
]

anyOf 任意条件 array

"anyOf": [
{ "maximum": 3 },
{ "type": "integer" }
]

oneOf うちの一つ array

"oneOf": [
{ "maximum": 3 },
{ "type": "integer" }
]

not でない JSON Schema "not": { "type": "string" }
Encoding contentEncoding エンコード(rfc2945) string "contentEncoding": "base64"
contentMediaType メデイア(rfc2046) string

"contentMediaType": "text/plain; charset=utf8"

"contentMediaType": "text/html"

"contentMediaType": "image/png"

再利用

definitions

$ref

定義済みスキーマの再利用 object

{
"type": "array",
"items": { "$ref": "#/definitions/positiveInteger" },
"definitions": {
"positiveInteger": {
"type": "integer",
"exclusiveMinimum": 0
}
}
}

定義済みフォーマットキーワード"format"

format
定義
sample
instance
date-time RFC3399 "format": "date-time" "2018-05-22T15:45:18+09:00"
date RFC3399 "format": "date" "2018-05-22"
time RFC3399 "format": "time" "15:45:18+09:00"
email RFC5322 "format": "email" "[email protected]"
idn-email RFC6531 "format": "idn-email"
hostname RFC1034 "format": "hostname"
idn-hostname RFC1034 "format": "idn-hostname"
ipv4 RFC2673 "format": "ipv4"
ipv6 RFC4291 "format": "ipv6"
uri RFC3986 "format": "uri"
uri-reference RFC3986 "format": "uri-reference"
iri RFC3987 "format": "iri"
iri-reference RFC3987 "format": "iri-reference"
json-pointer RFC6901 "format": "json-pointer" "#/id/name"
relative-json-pointer "format": "relative-json-pointer"
regex ECMA262 "format": "regex" "[abc]+"

参考:

https://ajv.js.org/keywords.html

https://spacetelescope.github.io/understanding-json-schema/reference/object.html