What is the best way to securely map backend data following a request?

Let me start by mentioning that this particular situation can also occur in the opposite manner as well.

Situation: Imagine a scenario where the backend developer, who is not really your friend :D, defines a DTO (send JSON) with properties, for instance in JAVA. However, he does not engage in communication with you, which is definitely not the best practice.

Common issue: As a frontend developer working with ANGULAR, my main concern is ensuring the safety of mapping backend data to my frontend model (receive JSON). This aspect is often underestimated and may lead to complications.

Typical scenario: Although JSON Object mapping might seem like a great technique, there is still uncertainty regarding the location of the data within my program. It is crucial to have the data easily accessible.

Query: Is there any method or pattern available that would result in a runtime or compile-time error during the mapping process? This would provide assurance that the model matches exactly with the response data's JSON model.

Answer №1

Dealing with this issue personally, it appears that there is no established solution yet to address it in a straightforward and elegant manner.

One approach could be to validate the incoming JSON against the expected model properties, throwing an error if any unexpected properties are found. This validation can be accomplished using a recursive function for handling nested data structures by iterating through object keys.

Alternatively, a more sophisticated but elegant method is detailed in the article on "TypeScript Json Mapper", utilizing the "reflect-metadata api" for compatibility with angular 2+ applications.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rotation to a point in a circle using three.js

Currently, I am in possession of 2 instances of THREE.Vector3(). My task at hand is to create a circular shape around one vector with the second vector serving as a tangent. I have determined the radius of the circle geometry based on the distance betwee ...

What is the most efficient method for line wrapping in the react className attribute while utilizing Tailwind CSS with minimal impact on performance?

Is there a more efficient way to structure the className attribute when utilizing tailwind css? Which of the examples provided would have the least impact on performance? If I were to use an array for the classes and then join them together as shown in e ...

Having difficulties executing a JavaScript file in the command prompt

I'm having trouble running a JavaScript file in the command prompt. Can anyone assist me with this issue? D:\>Node Welcome to Node.js v12.14.1. Type ".help" for more information. > 001.js undefined > Node 001.js Thrown: Node 001.js ...

Issue with JQuery append function in IE9 when using JQuery version 1.7.1

Having some trouble integrating the Bootstrap-tag plugin into my app. It works smoothly with jQuery 1.8.3, as demonstrated on the GitHub project page. However, my application is using JQuery 1.7.1 and while it functions without any issues in Chrome, it&ap ...

"Uncovering a memory leakage issue within a recursive polling function in JavaScript

Current issue in need of resolution: My team and I are currently working on a large application that was initially released around two years ago. We are in the process of adding a new "always-on" status screen to the application, which will be the default ...

The data I am trying to obtain is not being returned by my hook as expected

I am trying to create a hook that fetches all properties from a database. The function I have is asynchronous, and when I call the hook in another function, it returns an empty array []. I understand that fetching data from the database is an asynchronous ...

Executing the serve command in an Angular project necessitates a project definition, which unfortunately could not be located

An issue has arisen with the application I developed (angular2 nodejs). Upon attempting to open it, I encountered the following error message: The serve command needs to be executed within an Angular project, however a project definition could not be lo ...

Encountering issues with the functionality of async await

I am a beginner when it comes to utilizing the async, await, and promise features in JavaScript. My current focus is on: async function sendTextMessage(text) { console.log("----1----"); var messageData = { message: { text: tex ...

Utilize jQuery script on every single element

Is there a way to implement a jquery function on elements that are dynamically loaded via ajax? <span class="h">Test</span><br /><br /> <span class="h">Test</span><br /><br /> <span class="h">Test</ ...

I am interested in preserving a QrCode by converting it into an image

Below is the updated code snippet: [HttpGet("GenerateQrCode")] public ActionResult<string> GenerateQrCode(string sellername, string vatregistration, string timestamp, string invoiceamount, string vatamoun) { string ltr = ((char)0x20 ...

The designated function name can be either "onButtonClick" or "onClickButton"

I am a Japanese developer working on web projects. Improving my English language skills is one of my goals. What would be the correct name for a function that indicates "when a button has been clicked"? Is it "onButtonClick"? Maybe "onClickButton"? Co ...

Dividing the body of md-tabs in Angular material

My current project involves using angular material (https://material.angularjs.org/) I'm curious if there is a method to separate a tabs body from the tab list in angular material. Unfortunately, I couldn't find any information regarding this in ...

Scrolling the bottom of a PDF object element in HTML using JavaScript - a simple tutorial

There is a Pdf object displayed on my website <object data="data/test.pdf" type="application/pdf" width="700" height="900"> </object> I'm trying to automatically scroll this pdf to the last page when the page loads. I've found tha ...

Trouble displaying JSON data in HTML using *ngFor in Angular

Just starting out with angular and I've created a services class that returns product details in JSON format. api.service.ts import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/ ...

Step-by-step guide to uploading files with Dredd using Swagger

I am trying to send the following data in my api.description.yml: parameters: - name: "file" in: "formData" required: true type: file description: fileupload consumes: - multipart/form-data; I am not certain about the ...

Searching for "unique elements" using AngularJS ng-repeat

I am trying to organize a list by category, but the challenge is that each category input is customized and can be added by any user in the list. My initial attempt involved using ng-repeat to filter out duplicate values (as seen in the code snippet uniqu ...

The specified type '{ rippleColor: any; }' cannot be assigned to type 'ColorValue | null | undefined'

I've been diving into learning reactnative (expo) with typescript, but I've hit a roadblock. TypeScript keeps showing me the error message Type '{ rippleColor: any; }' is not assignable to type 'ColorValue | null | undefined' ...

The Angular translation function is being called repeatedly without end

I have a function that performs text translation. It is hooked to all the text elements and appears as follows: $rootScope.translateText = function (key) { if (angular.isDefined(Language.dictionary[key])) { return Language.dictionary[key]; } ret ...

Retrieve JavaScript Variable Value when Button is Clicked via asp:HiddenField

Having limited experience with JavaScript and jQuery, I decided to make some modifications to a jQuery Slider for adjusting dates. You can check out what I've done so far here: http://jsfiddle.net/ryn_90/Tq7xK/6/. I managed to get the slider working ...

Do you have to change the style of each individual element using JavaScript, or is there another method to accomplish this?

Recently, I've been exploring different ways to use JavaScript to globally adjust styles. My goal is to modify the CSS rule that controls the element's style, similar to how you can do it using the Inspector in Webkit. However, after visiting htt ...