You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Common/Common/Documents/Validators.cs

61 lines
1.4 KiB

using Connected.Middleware;
using Connected.Security.Identity;
using Connected.Validation;
namespace Common.Documents;
public abstract class InsertDocumentValidator<TDocumentArgs> : MiddlewareComponent, IValidator<TDocumentArgs>
where TDocumentArgs : InsertDocumentArgs
{
public InsertDocumentValidator(IUserService users)
{
Users = users;
}
protected IUserService Users { get; }
protected TDocumentArgs Arguments { get; private set; }
public async Task Validate(TDocumentArgs args)
{
Arguments = args;
await ValidateAuthor();
}
protected virtual async Task OnValidating()
{
await Task.CompletedTask;
}
private async Task ValidateAuthor()
{
if (Arguments.Author is null)
return;
if (await Users.Select(Arguments.Author) is null)
throw new ArgumentException(nameof(Arguments.Author));
}
}
public abstract class UpdateDocumentValidator<TDocumentArgs, TPrimaryKey> : MiddlewareComponent, IValidator<TDocumentArgs>
where TDocumentArgs : UpdateDocumentArgs<TPrimaryKey>
where TPrimaryKey : notnull
{
public UpdateDocumentValidator(IUserService users)
{
Users = users;
}
protected IUserService Users { get; }
public async Task Validate(TDocumentArgs args)
{
await ValidateOwner(args);
}
private async Task ValidateOwner(TDocumentArgs args)
{
if (args.Owner is null)
return;
if (await Users.Select(args.Owner) is null)
throw new ArgumentException(nameof(args.Owner));
}
}