I have an endpoint that updates (PUTs) an entity. However, the entire class file is not required, only the property the requestor wants to change.
How do I get FluentValidation to just validate the properties that are being updated?
For background, my class is a simple “Products” class and when it is initially created (POSTed), the following validations happen successfully:
public class CreateProductCommandValidator:AbstractValidator<CreateProductCommand>
{
public CreateProductCommandValidator()
{
RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required");
RuleFor(x => x.Category).NotEmpty().WithMessage("Category is required");
RuleFor(x => x.ImageFile).NotEmpty().WithMessage("ImageFile is required");
RuleFor(x => x.Price).GreaterThan(0).WithMessage("Price must be greater than 0");
}
}
However, in my UpdateProductCommand
class, if the requestor only updates the Name
field, I just want to validate ONLY that field. Otherwise, the requestor would have to put in all of the properties again and that just feels redundant.