All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
- 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>
78 lines
3.1 KiB
C#
78 lines
3.1 KiB
C#
using FluentValidation;
|
|
using GermanApp.Application.DTOs;
|
|
|
|
namespace GermanApp.Presentation.Validators;
|
|
|
|
/// <summary>
|
|
/// Validator for CreateLevelDto.
|
|
/// Validates input when creating a new CEFR level.
|
|
/// </summary>
|
|
public class CreateLevelValidator : AbstractValidator<CreateLevelDto>
|
|
{
|
|
public CreateLevelValidator()
|
|
{
|
|
RuleFor(x => x.Name)
|
|
.NotEmpty().WithMessage("Level name is required.")
|
|
.MinimumLength(2).WithMessage("Level name must be at least 2 characters long.")
|
|
.MaximumLength(100).WithMessage("Level name must not exceed 100 characters.");
|
|
|
|
RuleFor(x => x.Code)
|
|
.NotEmpty().WithMessage("Level code is required.")
|
|
.MinimumLength(1).WithMessage("Level code must be at least 1 character long.")
|
|
.MaximumLength(10).WithMessage("Level code must not exceed 10 characters.")
|
|
.Matches("^[A-Za-z][A-Za-z0-9]*$").WithMessage("Level code must start with a letter and contain only letters and numbers.")
|
|
.Must(BeValidCefrCode).WithMessage("Level code must be a valid CEFR code (A1, A2, B1, B2, C1, C2).");
|
|
|
|
RuleFor(x => x.Order)
|
|
.GreaterThan(0).WithMessage("Order must be greater than 0.")
|
|
.LessThanOrEqualTo(100).WithMessage("Order must not exceed 100.");
|
|
}
|
|
|
|
private static bool BeValidCefrCode(string code)
|
|
{
|
|
// Normalize to uppercase for comparison
|
|
var upperCode = code.ToUpperInvariant();
|
|
|
|
// Valid CEFR levels
|
|
var validCodes = new[] { "A1", "A2", "B1", "B2", "C1", "C2" };
|
|
|
|
return validCodes.Contains(upperCode);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validator for UpdateLevelDto.
|
|
/// Validates input when updating an existing CEFR level.
|
|
/// </summary>
|
|
public class UpdateLevelValidator : AbstractValidator<UpdateLevelDto>
|
|
{
|
|
public UpdateLevelValidator()
|
|
{
|
|
RuleFor(x => x.Name)
|
|
.NotEmpty().WithMessage("Level name is required.")
|
|
.MinimumLength(2).WithMessage("Level name must be at least 2 characters long.")
|
|
.MaximumLength(100).WithMessage("Level name must not exceed 100 characters.");
|
|
|
|
RuleFor(x => x.Code)
|
|
.NotEmpty().WithMessage("Level code is required.")
|
|
.MinimumLength(1).WithMessage("Level code must be at least 1 character long.")
|
|
.MaximumLength(10).WithMessage("Level code must not exceed 10 characters.")
|
|
.Matches("^[A-Za-z][A-Za-z0-9]*$").WithMessage("Level code must start with a letter and contain only letters and numbers.")
|
|
.Must(BeValidCefrCode).WithMessage("Level code must be a valid CEFR code (A1, A2, B1, B2, C1, C2).");
|
|
|
|
RuleFor(x => x.Order)
|
|
.GreaterThan(0).WithMessage("Order must be greater than 0.")
|
|
.LessThanOrEqualTo(100).WithMessage("Order must not exceed 100.");
|
|
}
|
|
|
|
private static bool BeValidCefrCode(string code)
|
|
{
|
|
// Normalize to uppercase for comparison
|
|
var upperCode = code.ToUpperInvariant();
|
|
|
|
// Valid CEFR levels
|
|
var validCodes = new[] { "A1", "A2", "B1", "B2", "C1", "C2" };
|
|
|
|
return validCodes.Contains(upperCode);
|
|
}
|
|
}
|