2017-03-27 74 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

相关问题