0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-01 19:12:57 +00:00
CleanArchitecture/CleanArchitecture.Domain/Extensions/Validation/CustomValidator.cs
Alexander Konietzko 39c720607f
Seal classes
2023-07-01 20:54:51 +02:00

38 lines
1.5 KiB
C#

using System.Text.RegularExpressions;
using CleanArchitecture.Domain.Errors;
using FluentValidation;
namespace CleanArchitecture.Domain.Extensions.Validation;
public static partial class CustomValidator
{
public static IRuleBuilderOptions<T, string> StringMustBeBase64<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must(x => IsBase64String(x));
}
private static bool IsBase64String(string base64)
{
base64 = base64.Trim();
return base64.Length % 4 == 0 && Base64Regex().IsMatch(base64);
}
public static IRuleBuilder<T, string> Password<T>(
this IRuleBuilder<T, string> ruleBuilder,
int minLength = 8,
int maxLength = 50)
{
var options = ruleBuilder
.NotEmpty().WithErrorCode(DomainErrorCodes.UserEmptyPassword)
.MinimumLength(minLength).WithErrorCode(DomainErrorCodes.UserShortPassword)
.MaximumLength(maxLength).WithErrorCode(DomainErrorCodes.UserLongPassword)
.Matches("[A-Z]").WithErrorCode(DomainErrorCodes.UserUppercaseLetterPassword)
.Matches("[a-z]").WithErrorCode(DomainErrorCodes.UserLowercaseLetterPassword)
.Matches("[0-9]").WithErrorCode(DomainErrorCodes.UserNumberPassword)
.Matches("[^a-zA-Z0-9]").WithErrorCode(DomainErrorCodes.UserSpecialCharPassword);
return options;
}
[GeneratedRegex("^[a-zA-Z0-9\\+/]*={0,3}$", RegexOptions.None)]
private static partial Regex Base64Regex();
}