问题描述
有人可以帮我解决这个问题吗?我正在尝试将 CustomAsync更改为MustAsync ,但是我无法使事情正常进行.下面是我的自定义方法
Could some one please help me to resolved this? i'm trying to change CustomAsync to MustAsync, but i couldn't make things to work. Below is my custom method
RuleFor(o => o).MustAsync(o => {
return CheckIdNumberAlreadyExist(o)
});
private static async Task<ValidationFailure> CheckIdNumberAlreadyExist(SaveProxyCommand command)
{
if (command.Id > 0)
return null;
using (IDbConnection connection = new SqlConnection(ConnectionSettings.LicensingConnectionString))
{
var param = new DynamicParameters();
param.Add("@idnumber", command.IdNumber);
var vehicle = await connection.QueryFirstOrDefaultAsync<dynamic>("new_checkDuplicateProxyIdNumber", param, commandType: CommandType.StoredProcedure);
return vehicle != null
? new ValidationFailure("IdNumber", "Id Number Already Exist")
: null;
}
}
推荐答案
要使其与FluentValidation的最新版本一起使用,我必须使用以下代码.
To make it work with the latest version of the FluentValidation, I had to use the codes like below.
RuleFor(ws => ws).MustAsync((x, cancellation) => UserHasAccess(x)).WithMessage("User doesn't have access to perform this action");
请注意此处的lambda表达式 MustAsync((x,Cancellation)=> UserHasAccess(x))
,否则我总是会报错,因为无法从'方法组转换到'Func< Worksheet,CancellationToken,Task< bool>>
Please notice the lambda expression here MustAsync((x, cancellation) => UserHasAccess(x))
, without this I was always getting an error as cannot convert from 'method group' to 'Func<Worksheet, CancellationToken, Task<bool>>
下面是我的自定义 UserHasAccess
函数.
Below is my custom UserHasAccess
function.
private async Task <bool> UserHasAccess(Worksheet worksheet) {
var permissionObject = await _dataProviderService.GetItemAsync(worksheet.FileItemId);
if (permissionObject is null) return false;
if (EditAccess(permissionObject.Permission)) return true;
return false;
}
这篇关于流利的验证将CustomAsync更改为MustAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!