#Optional update-dto fields

1 messages · Page 1 of 1 (latest)

eager loom
#

Hi, my update Dto is extending PartialType of the Create-dto, eventhough I get validation errors for my Patch request, why?

export class UpdateTripDto extends PartialType(CreateTripDto) {}
winter prism
#

What kind of validation errors? What does CreateTripDto look like?

eager loom
#

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"
}```
winter prism
#

Hmm, can you show your controller as well?

eager loom
#

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);
  }
winter prism
#

Hmm, and just to be 100% sure, how are you sending the request? Everything here is looking right so far

eager loom
#

from Postman

winter prism
#

Very strange. I would expect that to work

#

Any chance youcan share a repo that shows this issue?

eager loom
#

Do you mean a repo of my project, or to open an issue?

winter prism
#

Something minimal would be best

#

Just something to show this issue

eager loom
#

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

winter prism
#

Right, but that defaults the point of the PartialType helper

eager loom
#

I think that I will go for this solution now

austere dome
#

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 🙄 )

winter prism
#

Also, I'm the author of the answer is that StackOverflow post 😉

austere dome
#

LOL—Sorry for that @winter prism ,  I @ the wrong person 🤦

That response shoulda gone to OP @eager loom