DeutschLernen/GermanApp/Presentation/Validators/LessonValidators.cs
Lasse Rune Hansen a6021fb148
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat(backend/validation): add FluentValidation for DTOs and update controllers
- Add FluentValidation.AspNetCore package
- Create LevelValidators (CreateLevelValidator, UpdateLevelValidator)
- Create LessonValidators (CreateLessonValidator, UpdateLessonValidator)
- Register FluentValidation in Program.cs with auto-validation
- Update LevelsController and LessonsController to use IActionResult
- Make service methods virtual for Moq testing compatibility
- Add 26 integration tests for LevelsController (13 tests)
- Add 34 integration tests for LessonsController (17 tests)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-09 17:35:14 +02:00

64 lines
2.5 KiB
C#

using FluentValidation;
using GermanApp.Application.DTOs;
namespace GermanApp.Presentation.Validators;
/// <summary>
/// Validator for CreateLessonDto.
/// Validates input when creating a new lesson.
/// </summary>
public class CreateLessonValidator : AbstractValidator<CreateLessonDto>
{
public CreateLessonValidator()
{
RuleFor(x => x.Title)
.NotEmpty().WithMessage("Lesson title is required.")
.MinimumLength(3).WithMessage("Lesson title must be at least 3 characters long.")
.MaximumLength(200).WithMessage("Lesson title must not exceed 200 characters.");
RuleFor(x => x.Description)
.MaximumLength(2000).WithMessage("Lesson description must not exceed 2000 characters.");
RuleFor(x => x.LevelId)
.GreaterThan(0).WithMessage("Level ID must be greater than 0.");
RuleFor(x => x.Order)
.GreaterThanOrEqualTo(0).WithMessage("Order must be at least 0.")
.LessThanOrEqualTo(1000).WithMessage("Order must not exceed 1000.");
RuleFor(x => x.Topic)
.NotEmpty().WithMessage("Topic is required.")
.MinimumLength(2).WithMessage("Topic must be at least 2 characters long.")
.MaximumLength(100).WithMessage("Topic must not exceed 100 characters.");
}
}
/// <summary>
/// Validator for UpdateLessonDto.
/// Validates input when updating an existing lesson.
/// </summary>
public class UpdateLessonValidator : AbstractValidator<UpdateLessonDto>
{
public UpdateLessonValidator()
{
RuleFor(x => x.Title)
.NotEmpty().WithMessage("Lesson title is required.")
.MinimumLength(3).WithMessage("Lesson title must be at least 3 characters long.")
.MaximumLength(200).WithMessage("Lesson title must not exceed 200 characters.");
RuleFor(x => x.Description)
.MaximumLength(2000).WithMessage("Lesson description must not exceed 2000 characters.");
RuleFor(x => x.LevelId)
.GreaterThan(0).WithMessage("Level ID must be greater than 0.");
RuleFor(x => x.Order)
.GreaterThanOrEqualTo(0).WithMessage("Order must be at least 0.")
.LessThanOrEqualTo(1000).WithMessage("Order must not exceed 1000.");
RuleFor(x => x.Topic)
.NotEmpty().WithMessage("Topic is required.")
.MinimumLength(2).WithMessage("Topic must be at least 2 characters long.")
.MaximumLength(100).WithMessage("Topic must not exceed 100 characters.");
}
}