'Express Typescript Prevent Unwanted Data In Post Request

the post request has content_Name,content_Type, content_Json.

Those are the field that should be sent, but if i add a property ill be able to send a request with an unwanted datafield how can i prevent that:

enter image description here

DTO:

content_name,
content_type,
Content_json,

Json Request (Example) sent

{
  "this_field_shouldnt_be_available" : "Zebb"
}

Request received

{
  "this_field_shouldnt_be_available" : "Zebb",
  content_name: null,
  content_type: null,
  Content_json: null
}

How can I have an exception to use a "DTO" to only allow the valid data is it possible without schema? just comparing to DTO?



Solution 1:[1]

You can add a middleware with a validation to verify each property key that must exist in the DTO, and then validate the quantity of keys of the DTO assuring that it is not greater than the keys you set to be there

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vinicius Silva