每個人!我感謝您的幫助。這是我第一次創建模式,它涉及航班資料,但我遇到了多個錯誤,因為在某些記錄中,它不涉及特定欄位(在本例中為 dst_airport)。
這是我包括的內容:
"dst_airport": {
"type": "object",
"properties": {
"airport_id": {
"type": "number"
},
"name": {
"type": "string"
},
"city": {
"type": "string"
},
"country": {
"type": "string"
},
"iata": {
"type": "string"
},
"iaco": {
"type": "string"
},
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"altitude": {
"type": "number"
},
"timezone": {
"type": "number"
},
"dst": {
"type": "string"
},
"tz_id": {
"type": "string"
},
"type": {
"type": "string"
},
"source": {
"type": "string"
},
"anyOf": [
{
"required": [
"dst_airport"
]
}
]
}
}
我添加了我在網上找到的試圖修復它的 anyof 部分,但我認為它實際上并沒有做任何事情。
有人可以伸出援手嗎?謝謝!
uj5u.com熱心網友回復:
required應該與properties定義它們的位置相同。例子 :
import jsonschema
schema = {
"type": "object",
"properties": {
"src_airport": {
"type": "object",
"properties": {
"airport_id": {"type": "number"}
}
},
"dst_airport": {
"type": "object",
"properties": {
"airport_id": {"type": "number"}
}
},
},
"required": [
"src_airport",
# do not put `dst_airport` here, meaning it is optional
]
}
example1 = {
"src_airport": {"airport_id": 1},
"dst_airport": {"airport_id": 2}
}
example2 = {
"src_airport": {"airport_id": 1},
# "dst_airport": {"airport_id": 2}
}
example3 = {
# "src_airport": {"airport_id": 1},
"dst_airport": {"airport_id": 2}
}
try:
jsonschema.validate(example1, schema=schema)
except jsonschema.exceptions.ValidationError:
print("example1: fail")
else:
print("example1: pass")
try:
jsonschema.validate(example2, schema=schema)
except jsonschema.exceptions.ValidationError:
print("example2: fail")
else:
print("example2: pass")
try:
jsonschema.validate(example3, schema=schema)
except jsonschema.exceptions.ValidationError:
print("example3: fail")
else:
print("example3: pass")
我也對 jsonschema 語法感到困惑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/412082.html
標籤:
