【问题标题】:How to throw an exception, if an object structure is not matching/fitting another one如果对象结构不匹配/适合另一个对象结构,如何抛出异常
【发布时间】:2017-03-27 23:58:28
【问题描述】:

我将读取格式正确的用户输入对象。

也就是说,输入对象现在可以具有接口中未定义的任何键或子结构。

如果用户提供了无效对象,我怎么能抛出异常?

预定义接口

  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"
        }
      ]
    }
  ]
}

【问题讨论】:

    标签: javascript typescript ecmascript-6


    【解决方案1】:

    TypeScript 只会在编译时强制执行类型。如果你想进行这种验证,你需要使用某种 json-schema 验证库。比如这个:https://github.com/epoberezkin/ajv

    更新

    例如,使用这个库 (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

    【讨论】:

    • 看看,我更新了答案,我认为这段代码对你有用。您只需进行所需的更改即可将其添加到您的代码中。
    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 2020-08-07
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多