Body parser ignores Dto structure.
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See:
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.