[英]How to include $id fields in Pydantic.schema()
根據json-schema.org ,最佳實踐是將 $id 字段包含在對象中。
例如,我正在努力解決如何在頂層獲得它;
class MySchema(BaseModel):
id: str = Field(default="http://my_url/my_schema.json", alias="$id")
if __name__ == '__main__':
pprint(MySchema.schema())
產量
{'properties': {'$id': {'default': 'http://my_url/my_schema.json',
'title': '$Id',
'type': 'string'}},
'title': 'MySchema',
'type': 'object'}
如何在頂層獲得 $id ,帶有標題和類型,而不是作為嵌套屬性?
Pydantic 提供了多種模式定制方式。 例如,使用 schema_extra 配置選項:
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
class Config:
schema_extra = {
'$id': "my.custom.schema"
}
print(Person.schema_json(indent=2))
輸出:
{
"title": "Person",
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"age": {
"title": "Age",
"type": "integer"
}
},
"required": [
"name",
"age"
],
"$id": "my.custom.schema"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.