JSON Schema关键字和语法的完整参考指南
$schemaCore / Meta适用于: rootDeclares the JSON Schema dialect version
{ "$schema": "https://json-schema.org/draft/2020-12/schema" }ℹ Always include at root to declare the dialect
$idCore / Meta适用于: anySets the base URI for the schema
{ "$id": "https://example.com/schemas/user.json" }titleCore / Meta适用于: anyHuman-readable title for the schema or property
{ "title": "User", "type": "object" }descriptionCore / Meta适用于: anyHuman-readable description of the schema's purpose
{ "description": "A registered user account", "type": "object" }$commentCore / Meta适用于: anyDeveloper comment — not rendered to users
{ "$comment": "Added 2024-01 to support OAuth users" }$defs / definitionsCore / Meta适用于: rootRepository for reusable sub-schemas
{ "$defs": { "email": { "type": "string", "format": "email" } } }ℹ $defs is draft 2020-12+; use definitions in draft-07
$refCore / Meta适用于: anyReference to another schema by URI or JSON Pointer
{ "$ref": "#/$defs/email" }ℹ Can reference internal (#/$defs/…) or external schemas
type: stringTypes适用于: stringValue must be a JSON string
{ "type": "string" }type: numberTypes适用于: numberValue must be a JSON number (integer or float)
{ "type": "number" }type: integerTypes适用于: numberValue must be a whole number (no fractional part)
{ "type": "integer" }type: booleanTypes适用于: booleanValue must be true or false
{ "type": "boolean" }type: nullTypes适用于: nullValue must be JSON null
{ "type": "null" }type: arrayTypes适用于: arrayValue must be a JSON array
{ "type": "array" }type: objectTypes适用于: objectValue must be a JSON object
{ "type": "object" }type: [multiple]Types适用于: anyValue may be one of the listed types
{ "type": ["string", "null"] }ℹ Useful for optional/nullable fields
minLength / maxLengthStrings适用于: stringMinimum and maximum number of Unicode code points
{ "type": "string", "minLength": 1, "maxLength": 100 }patternStrings适用于: stringValue must match a ECMA 262 regular expression
{ "type": "string", "pattern": "^[a-z0-9_]+$" }ℹ Partial match — use ^ and $ to anchor
formatStrings适用于: stringSemantic validation hint (implementation-dependent)
{ "type": "string", "format": "email" }ℹ Common values: email, uri, uuid, date, date-time, ipv4, ipv6, hostname
enumStrings适用于: anyValue must be one of the listed values
{ "type": "string", "enum": ["active", "inactive", "pending"] }constStrings适用于: anyValue must exactly equal the given value
{ "const": "v1" }ℹ Equivalent to enum with a single value
minimum / maximumNumbers适用于: numberInclusive lower and upper bounds
{ "type": "number", "minimum": 0, "maximum": 100 }exclusiveMinimum / exclusiveMaximumNumbers适用于: numberExclusive lower and upper bounds (value < max, value > min)
{ "type": "number", "exclusiveMinimum": 0 }ℹ In draft-07 these were booleans; draft 2019+ they are numbers
multipleOfNumbers适用于: numberValue must be a multiple of the given number
{ "type": "integer", "multipleOf": 5 }propertiesObjects适用于: objectSchema for each named property
{ "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } } }requiredObjects适用于: objectArray of property names that must be present
{ "type": "object", "required": ["id", "email"] }additionalPropertiesObjects适用于: objectSchema or boolean controlling unknown properties
{ "type": "object", "additionalProperties": false }ℹ false = no extra properties allowed; a schema validates extra properties
patternPropertiesObjects适用于: objectSchemas applied to properties whose name matches the regex key
{ "patternProperties": { "^S_": { "type": "string" } } }minProperties / maxPropertiesObjects适用于: objectMin/max number of properties in the object
{ "type": "object", "minProperties": 1, "maxProperties": 10 }items (draft-07)Arrays适用于: arraySchema for all items (or tuple schemas as array)
{ "type": "array", "items": { "type": "string" } }ℹ In draft-2020-12, items applies to all remaining items after prefixItems
prefixItems (2020-12)Arrays适用于: arrayPer-index schemas for a tuple
{ "type": "array", "prefixItems": [{ "type": "string" }, { "type": "number" }] }ℹ Replaces tuple form of items from draft-07
minItems / maxItemsArrays适用于: arrayMin/max number of array elements
{ "type": "array", "minItems": 1, "maxItems": 50 }uniqueItemsArrays适用于: arrayAll items must be unique
{ "type": "array", "items": { "type": "string" }, "uniqueItems": true }containsArrays适用于: arrayAt least one item must match the given schema
{ "type": "array", "contains": { "const": "admin" } }ℹ Use minContains / maxContains to control how many must match
allOfComposition适用于: anyValue must be valid against ALL listed schemas
{ "allOf": [{ "type": "string" }, { "minLength": 5 }] }ℹ Intersection — use for schema composition/extension
anyOfComposition适用于: anyValue must be valid against AT LEAST ONE listed schema
{ "anyOf": [{ "type": "string" }, { "type": "number" }] }oneOfComposition适用于: anyValue must be valid against EXACTLY ONE listed schema
{ "oneOf": [{ "type": "string" }, { "type": "number" }] }ℹ Validates all and ensures exactly one matches — slower than anyOf
notComposition适用于: anyValue must NOT be valid against the given schema
{ "not": { "type": "null" } }if / then / elseComposition适用于: anyConditional validation: if condition matches, then/else applies
{ "if": { "properties": { "type": { "const": "email" } } }, "then": { "required": ["email"] }, "else": { "required": ["phone"] } }{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/user.json",
"title": "User",
"description": "A registered user account",
"type": "object",
"required": ["id", "email", "role"],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"email": {
"type": "string",
"format": "email",
"maxLength": 255
},
"name": {
"type": ["string", "null"],
"maxLength": 100
},
"role": {
"type": "string",
"enum": ["admin", "editor", "viewer"]
},
"tags": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"maxItems": 20
},
"createdAt": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"address": {
"type": "object",
"required": ["street", "city", "country"],
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string", "pattern": "^[0-9]{5}$" },
"country":{ "type": "string", "minLength": 2, "maxLength": 2 }
}
}
},
"type": "object",
"properties": {
"billing": { "$ref": "#/$defs/address" },
"shipping": { "$ref": "#/$defs/address" }
}
}JSON Schema关键字是定义数据类型、格式、约束等的标准属性,包括$ref、properties、required、type等。
使用$ref关键字可以引用其他Schema定义,创建可复用的Schema组件。
properties定义对象的属性,而required是一个数组,指定其中哪些属性是必填的。
JSON Schema经历了Draft-04、Draft-07、2019-09、2020-12等多个版本的演进,每个版本都添加了新的关键字和功能。