| | | 1 | | using Enjoy.Domain.Shared.Errors; |
| | | 2 | | using Enjoy.Domain.Shared.Extensions; |
| | | 3 | | using Enjoy.Domain.Shared.Results; |
| | | 4 | | |
| | | 5 | | namespace Enjoy.Domain.Users.ValueObjects; |
| | | 6 | | |
| | | 7 | | public readonly record struct PasswordHash |
| | | 8 | | { |
| | 11 | 9 | | public string Value { get; } |
| | | 10 | | |
| | | 11 | | public const int MinLength = 8; |
| | | 12 | | public const int MaxLength = 500; |
| | | 13 | | |
| | 60 | 14 | | private PasswordHash(string value) => Value = value; |
| | | 15 | | |
| | | 16 | | public static Result<PasswordHash> Create(string? value) |
| | 69 | 17 | | { |
| | 69 | 18 | | if (string.IsNullOrWhiteSpace(value)) |
| | 7 | 19 | | return Result<PasswordHash>.Failure(Error.PasswordHash.IsNullOrEmpty); |
| | | 20 | | |
| | 62 | 21 | | var normalized = value.Trim(); |
| | 62 | 22 | | List<Error> errors = []; |
| | 62 | 23 | | if (normalized.Length < MinLength) |
| | 1 | 24 | | errors.Add(Error.PasswordHash.TooShort(MinLength)); |
| | 62 | 25 | | if (normalized.Length > MaxLength) |
| | 1 | 26 | | errors.Add(Error.PasswordHash.TooLong(MaxLength)); |
| | | 27 | | |
| | 62 | 28 | | return errors.IsEmpty() |
| | 62 | 29 | | ? Result<PasswordHash>.Success(new(normalized)) |
| | 62 | 30 | | : Result<PasswordHash>.Failure(errors); |
| | 69 | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | public override string ToString() => Value; |
| | | 34 | | |
| | 1 | 35 | | public static implicit operator string(PasswordHash hash) => hash.Value; |
| | | 36 | | } |