How to throw an exception, if an object structure is not matching/fitting another one

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


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


  • Describe relationship in maximo 7.5
  • How to create a TFS alert for changes to the items' Stack Rank field
  • Get column names and values in for loop PHP
  • Cannot access sitecore item field via API
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • Uncaught TypeError: $(…).select2 is not a function
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Retrieving value from sql ExecuteScalar()
  • Can Jackson SerializationFeature be overridden per field or class?
  • SSO with signing and signature validation doesn't work
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Benchmarking RAM performance - UWP and C#
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • Trying to get generic when generic is not available
  • how does django model after text[] in postgresql [duplicate]
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal