Currently, I am in the process of assembling a sandwich. Whenever all the necessary details are provided to Nest, everything operates smoothly and flawlessly. However, my predicament arises when attempting to assign null (empty string) to an enum, resulting in failed validation.
// successful
const sandwich = {
name: 'Turkey',
...
pricing: {
requirePayment: true,
default: {
value: 2000,
unit: 'whole',
}
}
}
// fails validation
const sandwich = {
name: 'Turkey',
...
pricing: {
requirePayment: false, // AKA free sandwich
default: {
value: "",
unit: "",
}
}
}
// create-sandwich.dto.ts
@ApiProperty({
description: '',
example: '',
})
@ValidateNested({
each: true,
})
@Type(() => PricingInterface)
@IsNotEmpty()
readonly pricing: PricingInterface;
// pricing.interface.ts
@ApiProperty({
description: '',
example: '',
})
@ValidateNested({
each: true,
})
@Type(() => DefaultPricingInterface)
@IsOptional()
readonly default: DefaultPricingInterface;
// default-pricing.interface.ts
@ApiPropertyOptional({
description: '',
example: '',
})
@IsEnum(PriceUnit)
@IsOptional()
readonly unit: PriceUnit; // WHOLE, HALF
@ApiPropertyOptional({
description: '',
example: '',
})
@IsNumber()
@IsOptional()
readonly value: number;
An error message is being prompted:
"pricing.default.unit must be a valid enum value"
I comprehend the issue at hand, however, I'm uncertain on how to adhere to the validation rule. In cases where the sandwich is complimentary, no value will be designated to pricing.default.unit
. Despite setting the property as optional, I would prefer to maintain the validation if attainable. How can I permit unit
to be an empty string?
Your input or suggestions are warmly welcomed!