fix: `@IsNotEmpty` not working on non-existent field
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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).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.