如果一个对象结构不匹配/拟合另一个对象,那么如何抛出一个异常 [英] How to throw an exception, if an object structure is not matching/fitting another one

查看:166
本文介绍了如果一个对象结构不匹配/拟合另一个对象,那么如何抛出一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



也就是说,输入对象现在可以有任何键或子结构,而不是



如果用户给出无效的对象,我如何抛出异常?



预定界面



 导出界面InputStructureInterface {
tableName:string,
viewType:字符串
structureName:string,
sections:Array< Section>
}

接口部分{
name:string ,
fields:Array< Field>
}

接口字段{
fieldName:string,
relationType:string,
relationName:null,
fieldUi:FieldUi
}

接口FieldUi {
fieldType:string,
label:strin
}



有效的输入结构



此结构是定义的子集 InputStructureInterface

  {
tableName:User ,
viewType:列表视图,
structureName:personal_data_settings_list_view,
sections:[
{
name
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:列表视图,
structureName:personal_data_settings_list_view,
section:[
{
nameTHIS_IS_A_TYPO:null,
fields:[
{
fieldNameTHIS_IS_A_TYPO:Name
}
]
}
]
}


解决方案

TypeScript将在编译时强制执行类型。如果要进行这种验证,需要使用某种类型的json-schema验证库。像这样一个例子: https://github.com/epoberezkin/ajv



更新



例如,使用这个库( https://github.com/epoberezkin/ajv )你可以这样做:

  import *作为Ajv从'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],
附加属性:false
}
]
}
},
必需:[tableName,viewType,structName],
additionalProperties:false
};

const validate = ajv.compile(schema);
let valid = validate(data); //< - 在这里传递你的json对象

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

要安装库: npm install ajv


I will read user input objects which should be in well-formed.

That is, the input objects could now have any key or sub-structure that is not defined in the interface.

How could I throw an exception, if a user gives an invalid object?

Pre-defined interface

  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
  }

Valid input structure

This structure is a subset under the defined 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"
            },
          }
        ]
      }
    ]
  }

Invalid input structure

Because viewTypeTHIS_IS_A_TYPO, nameTHIS_IS_A_TYPO are not present on the interface

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

解决方案

The TypeScript will just enforce the types in compile time. If you want to make this kind of validations you need to use some kind of json-schema validation library. Like this one for example: https://github.com/epoberezkin/ajv

UPDATE

For example, using this library (https://github.com/epoberezkin/ajv) you can do something like this:

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);
}

To install the library: npm install ajv

这篇关于如果一个对象结构不匹配/拟合另一个对象,那么如何抛出一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆