#Optional update-dto fields
1 messages · Page 1 of 1 (latest)
What kind of validation errors? What does CreateTripDto look like?
Here is my create-trip.dto :
export class CreateTripDto {
@IsString()
title: string;
@IsString()
description: string;
@IsInt()
@Min(1)
@Max(10)
maxReservations: number;
@IsInt()
@Min(1)
minReservations: number;
@IsBoolean()
isPrivate: boolean;
@IsBoolean()
acceptReservations: boolean;
@IsBoolean()
acceptReservationsAutomatically: boolean;
@IsOptional()
@IsDate()
acceptReservationsUntil?: Date;
@IsBoolean()
onPromotion: boolean;
@IsBoolean()
showReservationCount: boolean;
@Matches(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/, {
message: 'Phone number is not valid',
})
phoneNumber: string;
@IsEmail()
email: string;
@IsOptional()
@IsString()
videoUrl?: string;
@IsEnum(TripType)
type: TripType;
@IsInt({ each: true })
cities: number[];
@ValidateNested({ each: true })
@Type(() => CreateDayDto)
dayDtos: CreateDayDto[];
@ValidateNested({ each: true })
@Type(() => CreateHighlightDto)
highlightDtos: CreateHighlightDto[];
@ValidateNested({ each: true })
@Type(() => CreateIncludedOptionDto)
includedOptionDtos: CreateIncludedOptionDto[];
}
I get :
"message": [
"description must be a string",
"maxReservations must not be greater than 10",
"maxReservations must not be less than 1",
"maxReservations must be an integer number",
"minReservations must not be less than 1",
"minReservations must be an integer number",
"isPrivate must be a boolean value",
"acceptReservationsAutomatically must be a boolean value",
"onPromotion must be a boolean value",
"showReservationCount must be a boolean value",
"Phone number is not valid",
"email must be an email",
"type must be one of the following values: CULTURE, NATURE, SPORT, AVENTURE, DETENTE, FAMILLE, AMIS",
"each value in cities must be an integer number"
}```
Hmm, can you show your controller as well?
Sure :
@Controller('trips')
export class TripsController {
constructor(private readonly tripsService: TripsService) {}
@Post()
create(@Body() createTripDto: CreateTripDto) {
return this.tripsService.create(createTripDto);
}
@Get()
findAll() {
return this.tripsService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.tripsService.findOne(+id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateTripDto: UpdateTripDto) {
return this.tripsService.update(+id, updateTripDto);
}
Hmm, and just to be 100% sure, how are you sending the request? Everything here is looking right so far
Very strange. I would expect that to work
Any chance youcan share a repo that shows this issue?
Do you mean a repo of my project, or to open an issue?
I could, but I think that I can't produce something minimal to test, since I'm using Docker images etc..
the issue could also Be fixed if I only create a new update dto class without extending the create one
and marking all the fields as optional
Right, but that defaults the point of the PartialType helper
I think that I will go for this solution now
not sure if you've resolve this issue by now @winter prism ; but I had a similar issue and this article really helped me out— https://stackoverflow.com/questions/68497436/nestjs-mapped-types-and-dto-usage
Each key in the original CreateTripDto class needs to be decorated with @ApiProperty.
export class CreateTripDto {
@ApiProperty()
@IsString()
title: string;
@ApiProperty()
@IsString()
description: string;
...
Basically, the scope for Reflect Metadata is bound to the @nest/swagger package — which both @PartialType and @ApiProperty are a part of. Once I added those decorators to my own Create dto class, did PartialTypes work as expected.
It's kind of obtuse how it's presented in the documentation — https://docs.nestjs.com/openapi/mapped-types; I see now that each is also decorated with @ApiProperty but since it's not explicitly called out, I completely overlooked it (NestJs only "hints" that PartialTypes is imported from the @nestjs/swagger package 🙄 )
I'm confused about the mapped types in NestJS.
The documentation says that PartialType create a new class making its validation decorators optional.
So, we use it in our validation pipes as we do w...
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
I never got a reproduction of the issue, so there was nothing for me to fix/verify
Also, I'm the author of the answer is that StackOverflow post 😉
LOL—Sorry for that @winter prism , I @ the wrong person 🤦
That response shoulda gone to OP @eager loom