This project has moved. For the latest updates, please go here.
1
Vote

FluentValidation not working for a null property

description

I'm using FluentValidation with IDataErrorInfo, and I have a validator defined as follows:
public class StsInfoValidator : AbstractValidator<StsInfo>
{
    public StsInfoValidator()
    {
        RuleFor(x => x.SomeProperty).Cascade(CascadeMode.StopOnFirstFailure)
            .NotNull().NotEmpty().WithMessage("SomeProperty is required.");

    }        
}
However, passing in a null property doesn't seem to trigger the validator:
#region IDataErrorInfo

public string this[string columnName]
{
    get
    {

        var validator = new StsInfoValidator();

        if (columnName.Equals("SomeProperty"))
        {
            // SomeProperty below is null
            if (validator.Validate(this, SomeProperty).Errors.Any())
                return validator.Validate(this, CampusNexusApiServer).Errors.FirstOrDefault().ErrorMessage;
            else
            {
                validator = null;
                return string.Empty;
            }
        }

        return string.Empty;
    }
}

#endregion

validator.Validate(this, SomeProperty).Errors in this example is always empty.

comments