added validators in project

This commit is contained in:
2026-03-10 09:52:31 +01:00
parent 2e7b9e5154
commit 9f858df5f8
17 changed files with 558 additions and 188 deletions

View File

@@ -0,0 +1,41 @@
using BookHive.DTO.Member;
using FastEndpoints;
using FluentValidation;
namespace BookHive.Validators.Members;
public class CreateMemberDtoValidator : Validator<CreateMemberDto>
{
public CreateMemberDtoValidator()
{
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage("Email is required.")
.EmailAddress()
.WithMessage("Email is invalid.")
.MaximumLength(255)
.WithMessage("Email cannot be longer than 255 characters.");
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("First name is required.")
.MaximumLength(100)
.WithMessage("First name cannot be longer than 100 characters.")
.MinimumLength(2)
.WithMessage("First name cannot be shorter than 2 characters.");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("Last name is required.")
.MaximumLength(100)
.WithMessage("Last name cannot be longer than 100 characters.")
.MinimumLength(2)
.WithMessage("Last name cannot be shorter than 2 characters.");
RuleFor(x => x.MembershipDate)
.NotEmpty()
.WithMessage("Membership date is required.")
.Must(d => d <= DateOnly.FromDateTime(DateTime.Now))
.WithMessage("Membership date cannot be in the future.");
}
}

View File

@@ -0,0 +1,45 @@
using BookHive.DTO.Member;
using FastEndpoints;
using FluentValidation;
namespace BookHive.Validators.Members;
public class UpdateMemberDtoValidator : Validator<UpdateMemberDto>
{
public UpdateMemberDtoValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0)
.WithMessage("Id is invalid.");
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage("Email is required.")
.EmailAddress()
.WithMessage("Email is invalid.")
.MaximumLength(255)
.WithMessage("Email cannot be longer than 255 characters.");
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("First name is required.")
.MaximumLength(100)
.WithMessage("First name cannot be longer than 100 characters.")
.MinimumLength(2)
.WithMessage("First name cannot be shorter than 2 characters.");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("Last name is required.")
.MaximumLength(100)
.WithMessage("Last name cannot be longer than 100 characters.")
.MinimumLength(2)
.WithMessage("Last name cannot be shorter than 2 characters.");
RuleFor(x => x.MembershipDate)
.NotEmpty()
.WithMessage("Membership date is required.")
.Must(d => d <= DateOnly.FromDateTime(DateTime.Now))
.WithMessage("Membership date cannot be in the future.");
}
}