question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Body parser ignores Dto structure.

See original GitHub issue

Bug Report

Current behavior

While using POST over controller and passing Body as an DTO object it seems like we can still manipulate whole object, is it expected behaviour? How to change it so only attributes within DTO will be acceptable by the server?

Input Code

dto

export class CreateInstanceDto {
  name: string;
}

instance.service function

  async create(instanceBody: CreateInstanceDto, user: User): Promise<Instance> {
    try {
      const instance = new this.instanceModel(instanceBody);
      instance.createdBy = user.mail;
      await instance.save();
      return instance;
    } catch (e) {
      throw new BadRequestException(e.message.toString());
    }
  }

instance.controller

  @UseGuards(AuthGuard('jwt'))
  @Post()
  create(@Body() instanceDto: CreateInstanceDto, @Request() req): Promise<Instance> {
    return this.instanceService.create(instanceDto, req.user);
  }

instance.interface

export interface Instance {
  id?: string;
  name: string;
  active: boolean;
  status: any;
  createdAt: Date;
  createdBy: string;
  configuration: any;
}

instance.schema

import * as mongoose from 'mongoose';

export const InstanceSchema = new mongoose.Schema({
  name: {required: true, type: String, index: {unique: true}},
  active: {type: Boolean, default: false},
  status: {type: Map, default: {
      database : false,
      environment : false,
      configuration : false,
      language : false,
    }},
  createdAt: {type: Date, default: Date.now},
  createdBy: {type: String},
  configuration: {type: Map, default: {}},
});

Expected behavior

Once I post anything else than name it should ignore all other inputs. Right now if I pass name:“xxx” and active:true, object xxx is created with active status as true instead of default false provided by the schema.

Environment


Nest version: 6.6.4
 
For Tooling issues:
- Node version:  v10.15.3
- Platform: Windows

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

0reactions
lock[bot]commented, Jan 7, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs - @Query() does not transform to DTO - Stack Overflow
I wrote a DTO map query params to an object and I'm using a ValidationPipe to validate and transform the data to my...
Read more >
Automatically Mapping DTO to Entity on Spring Boot APIs
Let's learn how ModelMapper can help us automate the mapping process of DTOs into entities on Spring Boot APIs.
Read more >
Validation | NestJS - A progressive Node.js framework
Parsing and validating arrays#. TypeScript does not store metadata about generics or interfaces, so when you use them in your DTOs, ValidationPipe may...
Read more >
How to ignore properties with System.Text.Json | Microsoft Learn
Ignore individual properties. To ignore individual properties, use the [JsonIgnore] attribute. The following example shows a type to serialize.
Read more >
Describing Request Body - Swagger
in: body; schema that describes the body data type and structure. ... It is required but ignored (it is used for documentation purposes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found