Skip to content

Commit

Permalink
refactor: make regexes and length variables as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
raczu committed Jun 5, 2024
1 parent fe7e7be commit 8a25fe8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 41 deletions.
32 changes: 22 additions & 10 deletions Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,43 @@ namespace ReasnAPI.Validators;

public class AddressValidator : AbstractValidator<AddressDto>
{
private const int MaxCountryLength = 64;
private const int MaxCityLength = 64;
private const int MaxStreetLength = 64;
private const int MaxStateLength = 64;
private const int MaxZipCodeLength = 8;

private const string CountryRegex = @"^\p{Lu}[\p{L}\s'-]*(?<![\s-])$";
private const string CityRegex = @"^\p{Lu}[\p{Ll}'.]+(?:[\s-][\p{L}'.]+)*$";
private const string StreetRegex = @"^[\p{L}\d\s\-/.,#']+(?<![-\s#,])$";
private const string StateRegex = @"^\p{Lu}\p{Ll}+(?:(\s|-)\p{L}+)*$";
private const string ZipCodeRegex = @"^[\p{L}\d\s-]{3,}$";

public AddressValidator()
{
RuleFor(a => a.Country)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{Lu}[\p{L}\s'-]*(?<![\s-])$");
.MaximumLength(MaxCountryLength)
.Matches(CountryRegex);

RuleFor(a => a.City)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{Lu}[\p{Ll}'.]+(?:[\s-][\p{L}'.]+)*$");
.MaximumLength(MaxCityLength)
.Matches(CityRegex);

RuleFor(a => a.Street)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^[\p{L}\d\s\-/.,#']+(?<![-\s#,])$");
.MaximumLength(MaxStreetLength)
.Matches(StreetRegex);

RuleFor(a => a.State)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{Lu}\p{Ll}+(?:(\s|-)\p{L}+)*$");
.MaximumLength(MaxStateLength)
.Matches(StateRegex);

RuleFor(r => r.ZipCode)
.MaximumLength(8)
.Matches(@"^[\p{L}\d\s-]{3,}$")
.MaximumLength(MaxZipCodeLength)
.Matches(ZipCodeRegex)
.When(r => !string.IsNullOrEmpty(r.ZipCode));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,48 @@ namespace ReasnAPI.Validators.Authentication;

public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
{
private const int MaxNameLength = 64;
private const int MaxSurnameLength = 64;
private const int MaxUsernameLength = 64;
private const int MaxEmailLength = 255;

private const string NameRegex = @"^\p{Lu}[\p{Ll}\s'-]+$";
private const string SurnameRegex = @"^\p{L}+(?:[\s'-]\p{L}+)*$";
private const string UsernameRegex = @"^[\p{L}\d._%+-]{4,}$";
private const string PasswordRegex = @"^((?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9]).{6,})\S$";
private const string PhoneRegex = @"^\+\d{1,3}\s\d{1,15}$";

public RegisterRequestValidator()
{
RuleFor(r => r.Name)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{Lu}[\p{Ll}\s'-]+$");
.MaximumLength(MaxNameLength)
.Matches(NameRegex);

RuleFor(r => r.Surname)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{L}+(?:[\s'-]\p{L}+)*$");
.MaximumLength(MaxSurnameLength)
.Matches(SurnameRegex);

RuleFor(r => r.Username)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^[\p{L}\d._%+-]{4,}$");
.MaximumLength(MaxUsernameLength)
.Matches(UsernameRegex);

RuleFor(r => r.Email)
.NotEmpty()
.MaximumLength(255)
.MaximumLength(MaxEmailLength)
.EmailAddress();

RuleFor(r => r.Password)
.NotEmpty()
.Matches(@"^((?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9]).{6,})\S$")
.Matches(PasswordRegex)
.WithMessage(
"Password must contain at least one uppercase letter, " +
"one lowercase letter, one number, and be at least 6 characters long.");

RuleFor(r => r.Phone)
.Matches(@"^\+\d{1,3}\s\d{1,15}$")
.Matches(PhoneRegex)
.When(r => !string.IsNullOrEmpty(r.Phone));

RuleFor(r => r.Address)
Expand Down
4 changes: 3 additions & 1 deletion Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace ReasnAPI.Validators;

public class CommentValidator : AbstractValidator<CommentDto>
{
private const int MaxContentLength = 1024;

public CommentValidator()
{
RuleFor(c => c.Content)
.NotEmpty()
.MaximumLength(1024);
.MaximumLength(MaxContentLength);
}
}
16 changes: 11 additions & 5 deletions Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ namespace ReasnAPI.Validators;

public class EventValidator : AbstractValidator<EventDto>
{
private const int MaxNameLength = 64;
private const int MaxDescriptionLength = 4048;
private const int MaxSlugLength = 128;

private const string SlugRegex = @"^[\p{L}\d]+[\p{L}\d-]*$";

public EventValidator()
{
RuleFor(e => e.Name)
.NotEmpty()
.MaximumLength(64);
.MaximumLength(MaxNameLength);

RuleFor(e => e.Description)
.NotEmpty()
.MaximumLength(4048);
.MaximumLength(MaxDescriptionLength);

RuleFor(e => e.StartAt)
.LessThan(e => e.EndAt)
.WithMessage("'StartAt' must be before 'EndAt'.");

RuleFor(e => e.Slug)
.NotEmpty()
.MaximumLength(128)
.Matches(@"^[\p{L}\d]+[\p{L}\d-]*$");

.MaximumLength(MaxSlugLength)
.Matches(SlugRegex);

RuleForEach(e => e.Tags)
.SetValidator(new TagValidator())
.When(e => e.Tags?.Count > 0);
Expand Down
8 changes: 6 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace ReasnAPI.Validators;

public class InterestValidator : AbstractValidator<InterestDto>
{
private const int MaxNameLength = 32;

private const string NameRegex = @"^\p{Lu}\p{Ll}+(?:\s\p{L}+)*$";

public InterestValidator()
{
RuleFor(i => i.Name)
.NotEmpty()
.MaximumLength(32)
.Matches(@"^\p{Lu}\p{Ll}+(?:\s\p{L}+)*$");
.MaximumLength(MaxNameLength)
.Matches(NameRegex);
}
}
14 changes: 10 additions & 4 deletions Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ namespace ReasnAPI.Validators;

public class ParameterValidator : AbstractValidator<ParameterDto>
{
private const int MaxKeyLength = 32;
private const int MaxValueLength = 64;

private const string KeyRegex = @"^\p{L}+(?:\s\p{L}+)*$";
private const string ValueRegex = @"^[\p{L}\d]+(?:\s[\p{L}\d]+)*$";

public ParameterValidator()
{
RuleFor(p => p.Key)
.NotEmpty()
.MaximumLength(32)
.Matches(@"^\p{L}+(?:\s\p{L}+)*$");
.MaximumLength(MaxKeyLength)
.Matches(KeyRegex);

RuleFor(p => p.Value)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^[\p{L}\d]+(?:\s[\p{L}\d]+)*$");
.MaximumLength(MaxValueLength)
.Matches(ValueRegex);
}
}
8 changes: 6 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace ReasnAPI.Validators;

public class TagValidator : AbstractValidator<TagDto>
{
private const int MaxNameLength = 64;

private const string NameRegex = @"^\p{L}+(?:\s\p{L}+)*$";

public TagValidator()
{
RuleFor(t => t.Name)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{L}+(?:\s\p{L}+)*$");
.MaximumLength(MaxNameLength)
.Matches(NameRegex);
}
}
26 changes: 18 additions & 8 deletions Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@ namespace ReasnAPI.Validators;

public class UserValidator : AbstractValidator<UserDto>
{
private const int MaxNameLength = 64;
private const int MaxSurnameLength = 64;
private const int MaxUsernameLength = 64;
private const int MaxEmailLength = 255;

private const string NameRegex = @"^\p{Lu}[\p{Ll}\s'-]+$";
private const string SurnameRegex = @"^\p{L}+(?:[\s'-]\p{L}+)*$";
private const string UsernameRegex = @"^[\p{L}\d._%+-]{4,}$";
private const string PhoneRegex = @"^\+\d{1,3}\s\d{1,15}$";

public UserValidator()
{
RuleFor(r => r.Name)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{Lu}[\p{Ll}\s'-]+$");
.MaximumLength(MaxNameLength)
.Matches(NameRegex);

RuleFor(r => r.Surname)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^\p{L}+(?:[\s'-]\p{L}+)*$");
.MaximumLength(MaxSurnameLength)
.Matches(SurnameRegex);

RuleFor(r => r.Username)
.NotEmpty()
.MaximumLength(64)
.Matches(@"^[\p{L}\d._%+-]{4,}$");
.MaximumLength(MaxUsernameLength)
.Matches(UsernameRegex);

RuleFor(r => r.Email)
.NotEmpty()
.MaximumLength(255)
.MaximumLength(MaxEmailLength)
.EmailAddress();

RuleFor(r => r.Phone)
.Matches(@"^\+\d{1,3}\s\d{1,15}$")
.Matches(PhoneRegex)
.When(r => !string.IsNullOrEmpty(r.Phone));
}
}

0 comments on commit 8a25fe8

Please sign in to comment.