English
Profile rules
Step 1. The error of the feature
Why. Every feature has its own static class of errors; for users, one is enough. Note the NotFound kind: the API answers 404, and the client knows that the id is wrong, not that it lacks a permission.
Do.
cs
using Chatex.Core.Common;
namespace Chatex.Core.Users;
public static class UserErrors
{
public static Error NotFound { get; } = Error.NotFound("user_not_found", "The user does not exist.");
}Step 2. Validators for the profile commands
Why. Two commands change a profile: UpdateProfileCommand(DisplayName, Bio) and ChangePasswordCommand(CurrentPassword, NewPassword), both defined in UserDtos.cs in topic 8. Their validators join the limits we created there. As with registration, the strength of the new password is checked by Identity, not here.
Do. Extend ProfileValidators.cs (new lines in green):
cs
using FluentValidation;
namespace Chatex.Core.Users;
internal static class ProfileValidators
{
public const int MaxDisplayName = 64;
public const int MaxBio = 500;
}
internal sealed class UpdateProfileCommandValidator : AbstractValidator<UpdateProfileCommand>
{
public UpdateProfileCommandValidator()
{
RuleFor(c => c.DisplayName).NotEmpty().MaximumLength(ProfileValidators.MaxDisplayName);
RuleFor(c => c.Bio).MaximumLength(ProfileValidators.MaxBio);
}
}
internal sealed class ChangePasswordCommandValidator : AbstractValidator<ChangePasswordCommand>
{
public ChangePasswordCommandValidator()
{
RuleFor(c => c.CurrentPassword).NotEmpty();
RuleFor(c => c.NewPassword).NotEmpty().MaximumLength(128);
}
} Nothing else is needed to use them: AddValidatorsFromAssemblyContaining in AddCore (topic 8) finds every validator of the assembly, including these two.
Check
dotnet build succeeds.