2

我有以下问题,我试图在 Windows Phone 中使用FluentValidation来验证字符串是否是有效的用户名。

我遇到的问题是验证器值具有string必须测试的类型。在所有示例中,我发现它总是像这样被测试的对象的属性RuleFor(customer => customer.Surname).NotEmpty();

当我运行此代码时,出现以下错误

代码:

public class UsernameValidator : AbstractValidator<string>
{
    public UsernameValidator()
    {
        RuleFor(username => username).NotNull().NotEmpty().WithMessage("Enter a username");
        RuleFor(username => username).Matches("^[a-zA-Z0-9_]*$").WithMessage("Only letters and numbers");
        RuleFor(username => username).Length(3, 30).WithMessage("Minimum length is 3");
    }
}

错误:

FluentValidation for WP7 can only be used with expressions that reference public properties, ie x => x.SomeProperty
   at FluentValidation.Internal.Extensions.CompilePropertyGetterExpression(LambdaExpression expression, Type delegateType)
   at FluentValidation.Internal.Extensions.Compile[TDelegate](Expression`1 expression)
   at FluentValidation.Internal.PropertyRule`1.Create[TProperty](Expression`1 expression, Func`1 cascadeModeThunk)
   at FluentValidation.AbstractValidator`1.RuleFor[TProperty](Expression`1 expression)
   at WorldChat.ViewModels.Validators.UsernameValidator..ctor()
   at WorldChat.ViewModels.RegisterViewModel.get_ErrorUsername()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] index, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at System.Windows.CLRPropertyListener.get_Value()
   at System.Windows.PropertyAccessPathStep.ConnectToPropertyInSource(Boolean isSourceCollectionViewCurrentItem)
   at System.Windows.PropertyAccessPathStep.ConnectToProperty()
   at System.Windows.PropertyAccessPathStep.ReConnect(Object newSource)
   at System.Windows.PropertyPathListener.ReConnect(Object source)
   at System.Windows.Data.BindingExpression.SourceAcquired()
   at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
   at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
   at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent, Byte bCanCreateParent)
   at MS.Internal.XcpImports.MeasureNative(IntPtr element, Single inWidth, Single inHeight)
   at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
   at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
   at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
   at Microsoft.Phone.Controls.PhoneApplicationFrame.MeasureOverride(Size availableSize)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
4

3 回答 3

2

我找到了一个解决方法,它需要你有一个额外的字符串包装类。

验证字符串.cs

public class ValidationString
{
    public string value { get; set; }

    public ValidationString(string value)
    {
        this.value = value;
    }
}

在用户名验证器类中,我添加了一个静态方法 IsUsername

public class UsernameValidator : AbstractValidator<ValidationString>
{
    public UsernameValidator()
    {
        RuleFor(username => username.value).NotNull().NotEmpty().WithMessage("Enter a username");
        RuleFor(username => username.value).Matches("^[a-zA-Z0-9_]*$").WithMessage("Only letters and numbers");
        RuleFor(username => username.value).Length(3, 30).WithMessage("Minimum length is 3");
    }

    internal static bool IsUsername(string value)
    {
        return new UsernameValidator().Validate(new ValidationString(value)).IsValid;
    }
}

现在我既可以在字符串上使用这个 UsernameValidator,也可以在另一个验证器上使用它,例如注册验证器

public class RegisterValidator : AbstractValidator<User>
{
    public RegisterValidator()
    {
        RuleFor(user => user.Username).Must(UsernameValidator.IsUsername);
        // some more rules
    }
}

如果您不想在其他验证类中使用您的 UsernameValidator,您可以忽略该IsUsername方法。

于 2012-05-19T13:58:33.527 回答
1

创建一个只有成员是要测试的东西的虚拟类怎么样?(好吧,为每种类型的数据说一个成员):

public class ForYourTestsOnly
{
    public string MyString { get; set;}
    public int MyInt32 { get; set;}
    //And so on...
}

接着 :

RuleFor(test => test.MyString)

(我承认我对 FluentValidation 一无所知,但如果它是让它工作的唯一方法,我想我会朝那个方向搜索......)

于 2012-05-19T13:00:32.230 回答
-1

问题与

RuleFor(username => username)

是 Fluent Validation 无法提取属性名称。Fluent Validation 期望验证属性,因此它尝试从RuleFor()参数中的表达式树中提取属性名称。由于您的表达式未提及属性,因此失败。

您可以通过显式指定属性来禁止 FV 尝试这样做。即使该属性不存在,这也有效,因为据我所知,FV 只需要它来报告验证错误。

所以以下对我有用,没有额外的ValidationString包装类:

public class StringNotEmptyValidator : AbstractValidator<string>
{
    public StringNotEmptyValidator()
    {
        RuleFor(str => str).NotEmpty().OverridePropertyName("StringValue");
    }
}

这是验证字符串不为空的示例。对于您的用户名验证案例,验证器看起来像这样(我没有对此进行测试,但概念是相同的):

public class UsernameValidator : AbstractValidator<string>
{
    public UsernameValidator()
    {
        RuleFor(username => username).NotNull().NotEmpty().WithMessage("Enter a username").OverridePropertyName("UsernameValue");
        RuleFor(username => username).Matches("^[a-zA-Z0-9_]*$").WithMessage("Only letters and numbers").OverridePropertyName("UsernameValue");
        RuleFor(username => username).Length(3, 30).WithMessage("Minimum length is 3").OverridePropertyName("UsernameValue");
    }
}
于 2012-06-13T07:48:16.370 回答