37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using FluentValidation;
|
|
using MealPlan.Api.DTOs.Recipe;
|
|
|
|
namespace MealPlan.Api.Validators;
|
|
|
|
public class CreateRecipeValidator : AbstractValidator<CreateRecipeDto>
|
|
{
|
|
public CreateRecipeValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty().MaximumLength(255);
|
|
RuleFor(x => x.MealCategory).InclusiveBetween(0, 3);
|
|
RuleFor(x => x.Calories).GreaterThanOrEqualTo(0).When(x => x.Calories.HasValue);
|
|
RuleFor(x => x.PrepTimeMinutes).GreaterThanOrEqualTo(0).When(x => x.PrepTimeMinutes.HasValue);
|
|
RuleForEach(x => x.Ingredients).SetValidator(new CreateIngredientValidator());
|
|
RuleForEach(x => x.Steps).SetValidator(new CreateRecipeStepValidator());
|
|
}
|
|
}
|
|
|
|
public class CreateIngredientValidator : AbstractValidator<CreateIngredientDto>
|
|
{
|
|
public CreateIngredientValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty().MaximumLength(255);
|
|
RuleFor(x => x.Unit).MaximumLength(50).When(x => x.Unit != null);
|
|
RuleFor(x => x.AmountGrams).GreaterThanOrEqualTo(0).When(x => x.AmountGrams.HasValue);
|
|
}
|
|
}
|
|
|
|
public class CreateRecipeStepValidator : AbstractValidator<CreateRecipeStepDto>
|
|
{
|
|
public CreateRecipeStepValidator()
|
|
{
|
|
RuleFor(x => x.StepNumber).GreaterThan(0);
|
|
RuleFor(x => x.Description).NotEmpty();
|
|
}
|
|
}
|