JSONSchema使用技巧

$data

比如:驗證重複密碼的時候的jsonschema

<code>{
"type": "object",
"properties": {
\t"email": {"type": "string", "format": "email"},
"password": {"type": "string"}
"repassword": {"type": "string", "const": { "$data": "1/password" }}
}
}/<code>

這裡repassword字段 const關鍵字使用了$data後面的值是一個relative JSON-pointer的值

relative JSON-pointer與JSON-pointer不同的是relative JSON-pointer是相對於自身位置的定位,'/'前面的數字表示相當於自己往上幾層,0表示自身.還可以使用'#'來引用鍵.

例如:

<code>"additionalProperties": {
"type": "string",
"format": { "$data": "0#" }
}/<code>
<code>{
'date-time': '1963-06-19T08:30:06.283185Z',
email: '[email protected]'
}
  /<code>

key和value格式對應通過驗證

遞歸

我們在驗證例如:

<code>{
"title": "title1",
\t"child":[
]
}/<code>

這種樹形結構的數據時,jsonschema:

<code>{
"definitions": {
"person": {
"type": "object",
"properties": {
"name": { "type": "string" },
"children": {
"type": "array",
"items": { "$ref": "#/definitions/person" },
"default": []
}
}
}
},
"type": "object",
"properties": {
"person": { "$ref": "#/definitions/person" }
}
}
/<code>

在children字段中$ref引用了父級具有相同結構的數據


分享到:


相關文章: