我正在嘗試撰寫一個 json 驗證器來在運行前檢查檔案,但是使用“type”會發生一個非常奇怪的問題。
我知道“型別”是一個保留字,但如果 jsonSchema 沒有我在另一個問題中發現的值配對,它就沒有問題:Key values of 'key' and 'type' in json schema。
他們的問題的解決方案是封裝"type": { "type": "string"}在“屬性”中,并且確實有效。但是,我的實作要求它位于陣列內。這是我的代碼片段:
{
"type": "object",
"additionalProperties": false,
"properties":{
"method":{
"type": "array",
"items":{
"type": {
"type": "string"
},
"name":{
"type": "string"
},
"provider": {
"type": "array"
}
}
}
}
}
奇怪的是,VScode 在隔離時在檔案中沒有問題,但是當它包含在主代碼中時,它不喜歡它并且沒有解決方案。無論如何,使用 python 驗證它會產生:
...
raise exceptions.SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: {'type': 'string'} is not valid under any of the given schemas
Failed validating 'anyOf' in metaschema['allOf'][1]['properties']['properties']['additionalProperties']['$dynamicRef']['allOf'][1]['properties']['items']['$dynamicRef']['allOf'][3]['properties']['type']:
{'anyOf': [{'$ref': '#/$defs/simpleTypes'},
{'items': {'$ref': '#/$defs/simpleTypes'},
'minItems': 1,
'type': 'array',
'uniqueItems': True}]}
On schema['properties']['method']['items']['type']:
{'type': 'string'}
更讓我困惑的是,https: //www.jsonschemavalidator.net/告訴我
Expected array or string for 'type', got StartObject. Path 'properties.method.items.type', line 8, position 17.JSON Schema Faker 能夠毫無問題地生成假檔案。使用 python 和 JSONSchemaValidator 驗證時,生成的假 json 也會回傳相同的錯誤。
我是初學者,任何幫助或見解將不勝感激,感謝您的時間。
編輯:這是所要求的輸入資料片段。
{
...
"method": [
{
"type": "action",
"name": "name of the chaos experiment to use here",
"provider": [
]
}
}
]
}
uj5u.com熱心網友回復:
陣列沒有屬性;陣列有專案。您包含的架構無效;你確定你不是故意的嗎?
{
"type": "object",
"additionalProperties": false,
"properties":{
"method":{
"type": "array",
"items":{
"type": "object",
"properties": {
"type": {
"type": "string"
},
"name":{
"type": "string"
},
"provider": {
"type": "array"
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436830.html
標籤:数组 json jsonschema
