Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
51 stars 28 forks source link

@ValidateNested() on disconnect property #44

Closed mirrom closed 3 months ago

mirrom commented 4 months ago

I have a prisma model that looks something like this:

model Group {
  /// @IsUUID
  uuid                  String        @id @default(uuid()) @db.Uuid
  /// @DtoRelationCanConnectOnCreate
  /// @DtoRelationCanConnectOnUpdate
  /// @DtoRelationCanDisconnectOnUpdate
  parentGroup           Group?        @relation("Groups", fields: [parentGroupUuid], references: [uuid])
  /// @DtoRelationIncludeId
  /// @DtoCreateHidden
  /// @DtoUpdateHidden
  /// @IsUUID
  parentGroupUuid       String?       @db.Uuid
  name                  String
}

This generates an UpdateGroupParentGroupRelationInputDto that looks like this:

export class UpdateGroupParentGroupRelationInputDto {
  @ApiProperty({
    required: false,
    nullable: true,
    type: ConnectGroupDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => ConnectGroupDto)
  connect?: ConnectGroupDto;
  @ApiProperty({
    required: false,
    nullable: true,
    type: Boolean,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => Boolean)
  disconnect?: boolean;
}

The disconnect property is annotated with @ValidateNested(). When I try to do a PATCH call with this body:

{
  "parentGroup": {
    "disconnect": true
  }
}

I get this response:

{
  "message": [
    "parentGroup.nested property disconnect must be either object or array"
  ],
  "error": "Bad Request",
  "statusCode": 400
}

When I manually remove the @ValidateNested() from the generated UpdateGroupParentGroupRelationInputDto, everything works fine. If I haven't overlooked anything, the @ValidateNested() should never be set for the disconnect property as it will always be a boolean, not a (nested) object or array, correct?

Brakebein commented 3 months ago

Thanks for the report! It's indeed a bug. The validator should be of course @IsBoolean(). Is now fixed with the latest release.

The disconnect property is boolean for n-to-one relationships (like in your case), but can also be a nested array for n-to-many relationships: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#disconnect-a-related-record

mirrom commented 3 months ago

Hey Brakebein,

thanks for the quick response!

Thanks for the report! It's indeed a bug. The validator should be of course @IsBoolean(). Is now fixed with the latest release.

I can confirm that this fixes the issue for me, thanks a lot!

The disconnect property is boolean for n-to-one relationships (like in your case), but can also be a nested array for n-to-many relationships: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#disconnect-a-related-record

This perfectly makes sense!