Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateDto missing null type for optional scalar fields #34

Closed
nawilson opened this issue Dec 18, 2023 · 2 comments
Closed

CreateDto missing null type for optional scalar fields #34

nawilson opened this issue Dec 18, 2023 · 2 comments

Comments

@nawilson
Copy link

Issue

The generated CreateDto does not include the null type for optional scalar fields.

Expected Behaviour

If a scalar field is nullable, the null type should be included in the CreateDto file.

The example below shows the middleName field being defined as optional in Prisma. Prisma treats this as automatic nullable in the database (and generates the correct type in @prisma/client).

However, the type generated in CreateDto is string instead of string | null.

Example:

schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

// https://github.com/Brakebein/prisma-generator-nestjs-dto
generator nestjsDto {
  provider                        = "prisma-generator-nestjs-dto"
  output                          = "../libs/shared/models/src/lib"
  outputToNestJsResourceStructure = "true"
  flatResourceStructure           = "false"
  exportRelationModifierClasses   = "true"
  reExport                        = "true"
  createDtoPrefix                 = "Create"
  updateDtoPrefix                 = "Update"
  dtoSuffix                       = "Dto"
  entityPrefix                    = ""
  entitySuffix                    = ""
  classValidation                 = "true"
  fileNamingStyle                 = "kebab"
  noDependencies                  = "false"
  outputType                      = "class"
  definiteAssignmentAssertion     = "true"
  requiredResponseApiProperty     = "true"
  prettier                        = "false"
}

model Person {
  id         Int     @id @unique @default(autoincrement())
  lastName   String  @map("last_name")
  firstName  String  @map("first_name")
  middleName String? @map("middle_name")
}

create-person.dto.ts

import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class CreatePersonDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  lastName!: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  firstName!: string;

  @ApiProperty({
    required: false,
    nullable: true,
  })
  @IsOptional()
  @IsString()
  middleName?: string;
}
@nawilson
Copy link
Author

See PR #27 by @zackdotcomputer

@Brakebein
Copy link
Owner

I finally merged this PR. New release v1.19.0 now online.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants