{
"status": 200,
"id": "123e4567-e89b-12d3-a456-426655440000",
"shop": {
"c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd": {
"123e4567-e89b-12d3-a456-426655443210": {
"quantity": {
"value": 10
}
},
"123e4567-e89b-12d3-a456-426655443211": {
"quantity": {
"value": 20
}
}
}
}
}
这是我的 json 响应。我想验证字段“c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd”,“123e4567-e89b-12d3-a456-426655443210”和“123e4567-e89b-12d3-a456-426655443211”,每次点击时都会唯一生成终点。
基于@pxcv7r的答案:
为了验证 UUID,您可以使用 JSON 模式中的格式,它为 UUID 语法提供内置支持:{ "type": "string", "format": "uuid" }
参见 https://json-schema.org/understanding-json-schema/reference/string.html
"propertyNames"
和 "unevaluatedProperties"
的组合来避免使用任何正则表达式:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"status": {
"type": "integer"
},
"id": {
"type": "string",
"format": "uuid"
},
"shop": {
"type": "object",
"minProperties": 1,
"maxProperties": 1,
"propertyNames": {
"format": "uuid"
},
"unevaluatedProperties": {
"type":"object",
"minProperties": 1,
"propertyNames": {
"format": "uuid"
},
"unevaluatedProperties": {
"title": "single variant of a shop",
"type": "object",
"properties": {
"quantity": {
"type": "object",
"properties": {
"value": {
"type": "integer"
}
}
}
}
}
}
}
}
}
要在 JSON 模式中验证字符串是否符合正则表达式模式,请使用
{ "type": "string", "pattern": "\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b" }
具体模式改编自问题 Searching for UUIDs in text with regex 请参阅此处了解更多详细信息。
特别要验证 UUID,您可以在 JSON 模式中使用
format
,它为 UUID 语法提供内置支持:{ "type": "string", "format": "uuid" }
参见 https://json-schema.org/understanding-json-schema/reference/string.html
您需要“patternProperties”:
{
"$schema":"http://json-schema.org/draft-07/schema#",
"type":"object",
"properties": {
"shop":{
"type":"object",
"additionalProperties":false,
"patternProperties":{
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}": {
"type":"object",
"patternProperties" :{
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}":{
"type":"object",
"properties":{
"quantity":{
"type":"object",
"properties":{
"value":{
"type":"integer"
}
}
}
}
}
}
}
}
}
}
}
Answer :`{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"required": [
"status",
"id",
"shop"
],
"additionalProperties": false,
"properties": {
"status": {
"type": "number"
},
"id": {
"type": "string"
},
"shop": {
"type": "object",
"patternProperties": {
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}": {
"type": "object",
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"patternProperties": {
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}": {
"type": "object",
"pattern": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"Properties": {
"type": "object",
"required": [
"quantity"
],
"additionalProperties": false,
"properties": {
"quantity": {
"type": "object",
"required": [
"value"
],
"additionalProperties": false,
"properties": {
"value": {
"type": "number"
}
}
}
}
}
}
}
}
}
}
}
}`