0

I'm encountering a challenge with my Prisma objects and could use some insights. I've defined the following Prisma objects, and my expectation is to optionally pass an id and perform an upsert. However, I'm consistently encountering the error message from Swagger:

"Missing field or invalid field type"

This occurs despite the id field being marked as optional in the definition. Any thoughts or suggestions on what might be causing this issue?

Thanks in advance for your help!

Model

model Vendor {
  id     String @id @unique() @default(uuid())
  vendor String @unique()
}
    // src/app-modules/vendor/dto/upsert-vendor.dto.ts
    
    import { ApiProperty } from '@nestjs/swagger';
    import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
    
    export class VendorDto {
      @ApiProperty({ example: 'Vendor Name' })
      @IsNotEmpty()
      @IsString()
      vendor: string;
      @ApiProperty({ example: '1', required: false })
      @IsOptional()
      id?: string;

}
// src/app-modules/vendor/vendor.controller.ts

import { Controller, Get, Post, Body } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { VendorService } from './vendor.service';
import { VendorDto } from './dto/vendor.dto';

@ApiTags('Vendor')
@Controller('vendor')
export class VendorController {
  constructor(private readonly vendorService: VendorService) {}

  @Get()
  @ApiOperation({ summary: 'Get all vendors' })
  @ApiResponse({
    status: 200,
    description: 'Returns all vendors',
  })
  findAll() {
    return this.vendorService.findAll();
  }

  @Post('upsert')
  @ApiOperation({ summary: 'Upsert a vendor' })
  @ApiResponse({
    status: 201,
    description: 'Vendor has been upserted successfully',
  })
  upsert(@Body() upsertVendorDto: VendorDto) {
    return this.vendorService.upsert(upsertVendorDto);
  }
}

Repository:

import { PrismaService } from '../prisma/prisma.service';
import { VendorDto } from './dto/vendor.dto';

@Injectable()
export class VendorRepository {
  constructor(private prisma: PrismaService) {}

  async findAll() {
    return this.prisma.vendor.findMany();
  }

  async upsert({ vendor, id }: VendorDto) {
    return this.prisma.vendor.upsert({
      where: { id },
      update: { vendor },
      create: { vendor },
    });
  }
}

Service


import { Injectable } from '@nestjs/common';
import { VendorDto } from './dto/vendor.dto';
import { VendorRepository } from './vendor.repository';

@Injectable()
export class VendorService {
  constructor(private vendorRepository: VendorRepository) {}

  async findAll() {
    return await this.vendorRepository.findAll();
  }

  async upsert(data: VendorDto) {
    return this.vendorRepository.upsert(data);
  }
}

2 Answers 2

0

Swagger uses the provided example values to generate sample requests; if the type doesn't match, it could lead to validation issues.

Can you change the example value for the id in VendorDto to be a valid UUID format instead of '1', since Prisma's id field is a UUID?

For example:

@ApiProperty({ example: '550e8400-e29b-41d4-a716-446655440000', required: false })
@IsOptional()
@IsString()
id?: string;
Sign up to request clarification or add additional context in comments.

Comments

0

Was able to fix it with this

import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { VendorDto } from './dto/vendor.dto';

@Injectable()
export class VendorRepository {
  constructor(private prisma: PrismaService) {}

  async findAll() {
    return await this.prisma.vendor.findMany();
  }

  async upsert({ name, domain, id }: VendorDto) {
    return await this.prisma.vendor.upsert({
      where: { id: id || '' },
      update: { name, domain },
      create: { name, domain },
    });
  }
  async delete(id: string) {
    return await this.prisma.vendor.delete({
      where: { id },
    });
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.