2017-03-27 19 views
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_TYPOnameTHIS_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" 
     } 
     ] 
    } 
    ] 
} 

回答

5

TypeScript將在編譯時執行這些類型。如果你想進行這種驗證,你需要使用某種json-schema驗證庫。像這樣的例子:https://github.com/epoberezkin/ajv

UPDATE

舉例來說,使用這個庫(https://github.com/epoberezkin/ajv),你可以做這樣的事情:

import * as Ajv from 'ajv'; 
const ajv = new Ajv(); 

const schema = { 
    "type": "object", 
    "properties": { 
     "tableName": { "type": "string" }, 
     "viewType": { "type": "string" }, 
     "structureName": { "type": "string" }, 
     "sections": { 
      "type": "array", 
      "items": [ 
       { 
        "type": "object", 
        "properties": { 
         "name": { "type": ["string", "null"] }, 
         "fields": { 
          "type": "array", 
          "items": [ 
           { 
            "type": "object", 
            "properties": { 
             "fieldName": { "type": "string" }, 
             "relationType": { "type": ["string", "null"] }, 
             "relationName": { "type": ["string", "null"] }, 
             "fieldUi": { 
              "fieldType": { "type": "string" }, 
              "label": { "type": "string" } 
             } 
            }, 
            "required": ["fieldName", "relationType", "relationName"], 
            "additionalProperties": false 
           } 
          ] 
         } 
        }, 
        "required": ["name", "fields"], 
        "additionalProperties": false 
       } 
      ] 
     } 
    }, 
    "required": ["tableName", "viewType", "structureName"], 
    "additionalProperties": false 
}; 

const validate = ajv.compile(schema); 
let valid = validate(data); // <-- pass your json object here 

if (!valid) { 
    console.log(validate.errors); 
} 

要安裝庫:npm install ajv

+0

非常感謝您的信息支持。我會檢查它〜 – newBike

+0

看一看,我更新了答案,我認爲這段代碼對你有用。您只需進行所需的更改即可將其放入代碼中。 – Diullei

相關問題