diff --git a/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs index 3e3b7611..6f2b2efb 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs @@ -5,31 +5,43 @@ namespace ReasnAPI.Validators; public class AddressValidator : AbstractValidator { + 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'-]*(? a.Country) .NotEmpty() - .MaximumLength(64) - .Matches(@"^\p{Lu}[\p{L}\s'-]*(? 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\-/.,#']+(? 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)); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Validators/Authentication/RegisterRequestValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/Authentication/RegisterRequestValidator.cs index fb8dfeb2..255dfc93 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/Authentication/RegisterRequestValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/Authentication/RegisterRequestValidator.cs @@ -6,37 +6,48 @@ namespace ReasnAPI.Validators.Authentication; public class RegisterRequestValidator : AbstractValidator { + 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) diff --git a/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs index b3b8a2c5..9c2ce1aa 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs @@ -5,10 +5,12 @@ namespace ReasnAPI.Validators; public class CommentValidator : AbstractValidator { + private const int MaxContentLength = 1024; + public CommentValidator() { RuleFor(c => c.Content) .NotEmpty() - .MaximumLength(1024); + .MaximumLength(MaxContentLength); } } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs index 7d087ee0..bc4d4d51 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs @@ -5,15 +5,21 @@ namespace ReasnAPI.Validators; public class EventValidator : AbstractValidator { + 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) @@ -21,9 +27,9 @@ public EventValidator() 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); diff --git a/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs index b7db499c..2b485429 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs @@ -5,11 +5,15 @@ namespace ReasnAPI.Validators; public class InterestValidator : AbstractValidator { + 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); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs index b8ae96ae..54894136 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs @@ -5,16 +5,22 @@ namespace ReasnAPI.Validators; public class ParameterValidator : AbstractValidator { + 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); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs index 0ce02f41..641ab05a 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs @@ -5,11 +5,15 @@ namespace ReasnAPI.Validators; public class TagValidator : AbstractValidator { + 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); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs index 32295a0f..fa072029 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs @@ -5,30 +5,40 @@ namespace ReasnAPI.Validators; public class UserValidator : AbstractValidator { + 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)); } } \ No newline at end of file