2
我會讀取應該是格式正確的用戶輸入對象。如何拋出異常,如果一個對象結構不匹配/擬合另一個
也就是說,輸入對象現在可以具有未在界面中定義的任何鍵或子結構。
如果用戶給出無效對象,我該如何拋出異常?
預先定義的接口
export interface InputStructureInterface {
"tableName": string,
"viewType": string,
"structureName": string,
"sections": Array<Section>,
}
interface Section{
"name": string,
"fields": Array<Field>
}
interface Field{
"fieldName": string,
"relationType": string,
"relationName": null,
"fieldUi": FieldUi
}
interface FieldUi {
"fieldType": string,
"label": strin
}
有效輸入結構
這種結構下的定義InputStructureInterface
一個子集
{
"tableName": "User",
"viewType": "List View",
"structureName": "personal_data_settings_list_view",
"sections": [
{
"name": null,
"fields": [
{
"fieldName": "Name",
"relationType": null,
"relationName": null,
"fieldUi": {
"fieldType": "string",
"label": "Name"
},
}
]
}
]
}
無效的輸入結構
因爲viewTypeTHIS_IS_A_TYPO
,nameTHIS_IS_A_TYPO
是ñ不存在於接口上
{
"tableName": "User",
"viewTypeTHIS_IS_A_TYPO": "List View",
"structureName": "personal_data_settings_list_view",
"sections": [
{
"nameTHIS_IS_A_TYPO": null,
"fields": [
{
"fieldNameTHIS_IS_A_TYPO": "Name"
}
]
}
]
}
非常感謝您的信息支持。我會檢查它〜 – newBike
看一看,我更新了答案,我認爲這段代碼對你有用。您只需進行所需的更改即可將其放入代碼中。 – Diullei