question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fix: `@IsNotEmpty` not working on non-existent field

See original GitHub issue

Description

I would expect @IsNotEmpty to fail if the attribute is not included. For example, I have the following DTO

export class ServiceDto {
  @ApiProperty({ description: 'The type unique id of the service' })
  readonly id: number;
    
  @IsNotEmpty()
  @ApiProperty({ description: 'The type of service' })
  readonly type: string
}

export class CreateServiceDto extends PartialType(
  OmitType(ServiceDto, ['id'] as const),
) {}

When I pass the following object:

{ id: 1, type: "" }

validation works saying the field may not be empty however, when I pass the following object it will not complain:

{ id: 1 }

After reading the documentation I found that there is an @IsOptional decorator, so I would assume all other fields are mandatory by default if not set. After some experiments, this behavior is true for other decorators such as @IsNumber.

Reproduction

I tried it via the main app and the e2e app and I get the same behavior. I simply used it in the main.ts of my NestJS app

  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000)

where my DTO is called by my controller with the following code

  @ApiTags('Service')
  @ApiOperation({summary: "This will create a new service record"})
  @Post()
  @ApiCreatedResponse({ description: "Record created" , type: ServiceDto})
  create(@Body() createServiceDto: CreateServiceDto) {
    return this.serviceRepository.createService(createServiceDto)
  }

class-validator version: 0.12.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
braaarcommented, Nov 16, 2022

This is a weird way to extend classes I don’t even understand what PartialType and OmitType is in this example.

In case you’re curious, those are GraphQL mapped types used in NestJS to extend GraphQL classes. It’s kind of weird syntax, but the end result is that you can extend your graphql models and keep the decorator based metadata on the class properties. For example, you can use PartialType to make an input model that is identical to your view model, except that all the fields are optional (or nullable, in graphql terms).

0reactions
github-actions[bot]commented, Dec 17, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to removed the ??? Reference to non-existent field error
If not, then get rid of the braces and have it be just a normal structure. Then use fieldnames() to see what fields...
Read more >
JavaScript validation for empty input field - Stack Overflow
An input field can have whitespaces, we want to prevent that. ... function() { if( isEmpty(this.value) ) { console.log( "NAME is invalid (Empty)"...
Read more >
I have a simple trigger that is giving me an Invalid foreign key ...
On the Account Object, I have a field called Pipeline Rep (with an API name of ... The get method is not working...
Read more >
Not equals operator (!=) excludes results with empty values
Issues with non-existing or empty values for the searched field are excluded ... does not fix the behaviour described in this ticket), i.e....
Read more >
Solved: Patch -> Invalid argument type. Expecting a record...
Solved: I am getting the below error in patch function -The function ... the Product field from the secondary SharePoint list is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found