using FluentValidation;
using GermanApp.Application.DTOs;
namespace GermanApp.Presentation.Validators;
///
/// Validator for CreateLevelDto.
/// Validates input when creating a new CEFR level.
///
public class CreateLevelValidator : AbstractValidator
{
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);
}
}
///
/// Validator for UpdateLevelDto.
/// Validates input when updating an existing CEFR level.
///
public class UpdateLevelValidator : AbstractValidator
{
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);
}
}