Reputation: 13
I'm trying to validate a request using DTO. I need to validate if value is a number and not empty.
When I try to use only the decorator IsNumber() and pass a body with a empty property, validation fails. So, I tried to include the decorator IsNotEmpty() too, but this is not working apparently, because if I pass a empty property, the flow follows. My code is something like this:
export class CreateOrderShippingDto implements CreateOrderShippingDtoInterface {
@ApiProperty({
type: Number,
})
@IsNotEmpty()
@IsNumber()
readonly orderId: number;
}
Someone can help?
Upvotes: 1
Views: 6525
Reputation: 4278
IsNotEmpty
is a validator for string. From the doc:
Checks if given value is not empty (!== '', !== null, !== undefined).
Use IsDefined
instead to check if the value is set:
export class CreateOrderShippingDto implements CreateOrderShippingDtoInterface {
@ApiProperty({
type: Number,
})
@IsDefined()
@IsNumber()
readonly orderId: number;
}
As a side note, IsDefined
ignores the skipMissingProperties
property from the configuration so this is a safe way to enforce a value to be set.
Upvotes: 1