DEV Community

John
John

Posted on

Fluent validation

using FluentValidation;

namespace OnlineCourse.Application.Validators
{
// =========================================================
// 1. INSTRUCTOR VALIDATOR
// =========================================================

public class InstructorValidator : AbstractValidator<Instructor>
{
    public InstructorValidator()
    {
        RuleFor(x => x.InstructorName)
            .NotEmpty()
            .WithMessage("Instructor name is required.")
            .MaximumLength(150)
            .WithMessage("Instructor name cannot exceed 150 characters.");

        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required.")
            .EmailAddress()
            .WithMessage("Enter a valid email address.");

        RuleFor(x => x.Phone)
            .NotEmpty()
            .WithMessage("Phone number is required.")
            .Matches(@"^\d{10}$")
            .WithMessage("Phone number must contain exactly 10 digits.");

        RuleFor(x => x.Expertise)
            .NotEmpty()
            .WithMessage("Expertise is required.")
            .MaximumLength(500)
            .WithMessage("Expertise cannot exceed 500 characters.");

        RuleFor(x => x.Qualifications)
            .MaximumLength(500)
            .WithMessage("Qualifications cannot exceed 500 characters.")
            .When(x => !string.IsNullOrWhiteSpace(x.Qualifications));

        RuleFor(x => x.YearsOfExperience)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Years of experience cannot be negative.");

        RuleFor(x => x.Status)
            .NotEmpty()
            .WithMessage("Status is required.")
            .Must(x => x == "Active" || x == "Inactive")
            .WithMessage("Status must be Active or Inactive.");
    }
}


// =========================================================
// 2. COURSE VALIDATOR
// =========================================================

public class CourseValidator : AbstractValidator<Course>
{
    public CourseValidator()
    {
        RuleFor(x => x.CourseTitle)
            .NotEmpty()
            .WithMessage("Course title is required.")
            .MaximumLength(150)
            .WithMessage("Course title cannot exceed 150 characters.");

        RuleFor(x => x.CategoryId)
            .GreaterThan(0)
            .WithMessage("Category is required.");

        RuleFor(x => x.InstructorId)
            .GreaterThan(0)
            .WithMessage("Instructor is required.");

        RuleFor(x => x.Description)
            .NotEmpty()
            .WithMessage("Description is required.")
            .MaximumLength(2000)
            .WithMessage("Description cannot exceed 2000 characters.");

        RuleFor(x => x.DurationHours)
            .GreaterThan(0)
            .WithMessage("Duration must be greater than 0.");

        RuleFor(x => x.Price)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Price cannot be negative.");

        RuleFor(x => x.MaxStudents)
            .GreaterThanOrEqualTo(1)
            .WithMessage("Maximum students must be at least 1.");

        RuleFor(x => x.StartDate)
            .GreaterThan(DateTime.UtcNow)
            .WithMessage("Course start date must be in the future.");

        RuleFor(x => x.Status)
            .NotEmpty()
            .WithMessage("Status is required.")
            .Must(x =>
                x == "Draft" ||
                x == "Active" ||
                x == "Inactive" ||
                x == "Archived")
            .WithMessage(
                "Status must be Draft, Active, Inactive or Archived.");
    }
}


// =========================================================
// 3. LESSON VALIDATOR
// =========================================================

public class LessonValidator : AbstractValidator<Lesson>
{
    public LessonValidator()
    {
        RuleFor(x => x.CourseId)
            .GreaterThan(0)
            .WithMessage("Course is required.");

        RuleFor(x => x.Title)
            .NotEmpty()
            .WithMessage("Lesson title is required.");

        RuleFor(x => x.Description)
            .MaximumLength(1000)
            .WithMessage("Description cannot exceed 1000 characters.")
            .When(x => !string.IsNullOrWhiteSpace(x.Description));

        RuleFor(x => x.LessonOrder)
            .GreaterThan(0)
            .WithMessage("Lesson order must be greater than 0.");

        RuleFor(x => x.EstimatedDuration)
            .GreaterThan(0)
            .WithMessage("Estimated duration must be greater than 0.");
    }
}


// =========================================================
// 4. ENROLLMENT VALIDATOR
// =========================================================

public class EnrollmentValidator : AbstractValidator<Enrollment>
{
    public EnrollmentValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User is required.");

        RuleFor(x => x.CourseId)
            .GreaterThan(0)
            .WithMessage("Course is required.");

        RuleFor(x => x.AgreeToTerms)
            .Equal(true)
            .WithMessage(
                "You must agree to the Terms and Conditions.");

        RuleFor(x => x.SpecialRequirements)
            .MaximumLength(500)
            .WithMessage(
                "Special requirements cannot exceed 500 characters.")
            .When(x =>
                !string.IsNullOrWhiteSpace(x.SpecialRequirements));
    }
}


// =========================================================
// 5. LESSON PROGRESS VALIDATOR
// =========================================================

public class LessonProgressValidator
    : AbstractValidator<LessonProgress>
{
    public LessonProgressValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User is required.");

        RuleFor(x => x.LessonId)
            .GreaterThan(0)
            .WithMessage("Lesson is required.");

        RuleFor(x => x.EnrollmentId)
            .GreaterThan(0)
            .WithMessage("Enrollment is required.");
    }
}


// =========================================================
// 6. REVIEW VALIDATOR
// =========================================================

public class ReviewValidator : AbstractValidator<Review>
{
    public ReviewValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User is required.");

        RuleFor(x => x.CourseId)
            .GreaterThan(0)
            .WithMessage("Course is required.");

        RuleFor(x => x.Rating)
            .InclusiveBetween(1, 5)
            .WithMessage("Rating must be between 1 and 5.");

        RuleFor(x => x.Comment)
            .MaximumLength(1000)
            .WithMessage(
                "Comment cannot exceed 1000 characters.")
            .When(x =>
                !string.IsNullOrWhiteSpace(x.Comment));
    }
}


// =========================================================
// 7. REGISTER REQUEST
// =========================================================

public class RegisterRequest
{
    public string FullName { get; set; } = string.Empty;

    public string Email { get; set; } = string.Empty;

    public string Password { get; set; } = string.Empty;

    public string ConfirmPassword { get; set; } = string.Empty;

    public string PhoneNumber { get; set; } = string.Empty;

    public string Country { get; set; } = string.Empty;

    public string? LearningGoals { get; set; }
}


// =========================================================
// 8. REGISTER VALIDATOR
// =========================================================

public class RegisterRequestValidator
    : AbstractValidator<RegisterRequest>
{
    public RegisterRequestValidator()
    {
        RuleFor(x => x.FullName)
            .NotEmpty()
            .WithMessage("Full name is required.")
            .MaximumLength(100)
            .WithMessage(
                "Full name cannot exceed 100 characters.")
            .Matches(@"^[A-Za-z ]+$")
            .WithMessage(
                "Full name can contain only alphabets and spaces.");

        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required.")
            .EmailAddress()
            .WithMessage("Enter a valid email address.");

        RuleFor(x => x.Password)
            .NotEmpty()
            .WithMessage("Password is required.")
            .Length(8, 24)
            .WithMessage(
                "Password must be between 8 and 24 characters.")
            .Matches("[A-Z]")
            .WithMessage(
                "Password must contain at least one uppercase letter.")
            .Matches("[a-z]")
            .WithMessage(
                "Password must contain at least one lowercase letter.")
            .Matches("[0-9]")
            .WithMessage(
                "Password must contain at least one digit.")
            .Matches(@"[^a-zA-Z0-9]")
            .WithMessage(
                "Password must contain at least one special character.");

        RuleFor(x => x.ConfirmPassword)
            .NotEmpty()
            .WithMessage("Confirm password is required.")
            .Equal(x => x.Password)
            .WithMessage("Passwords do not match.");

        RuleFor(x => x.PhoneNumber)
            .NotEmpty()
            .WithMessage("Phone number is required.")
            .Matches(@"^\d{10}$")
            .WithMessage(
                "Phone number must contain exactly 10 digits.");

        RuleFor(x => x.Country)
            .NotEmpty()
            .WithMessage("Country is required.")
            .MaximumLength(100)
            .WithMessage(
                "Country cannot exceed 100 characters.");

        RuleFor(x => x.LearningGoals)
            .MaximumLength(500)
            .WithMessage(
                "Learning goals cannot exceed 500 characters.")
            .When(x =>
                !string.IsNullOrWhiteSpace(x.LearningGoals));
    }
}


// =========================================================
// 9. LOGIN REQUEST
// =========================================================

public class LoginRequest
{
    public string Email { get; set; } = string.Empty;

    public string Password { get; set; } = string.Empty;
}


// =========================================================
// 10. LOGIN VALIDATOR
// =========================================================

public class LoginRequestValidator
    : AbstractValidator<LoginRequest>
{
    public LoginRequestValidator()
    {
        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required.")
            .EmailAddress()
            .WithMessage("Enter a valid email address.");

        RuleFor(x => x.Password)
            .NotEmpty()
            .WithMessage("Password is required.");
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)