我有一个类Solicitor
,其中包含一个Address
对象。需要将该地址对象设置为有效的地址值,或将地址内的所有值都设置为TBC
,以表明该地址尚未确认。
因此,我想使用FluentValidator在律师对象上设置地址验证器,但是仅当地址的值未设置为“ TBC”时。
所以: 如果地址值为“ TBC”:不设置验证器。 如果地址值不是“ TBC”,则设置验证器。
我有一个测试类,该类创建一个律师,该律师持有一个包含“ TBD”值的地址对象,该对象应触发验证程序并导致其在律师的地址对象上设置地址验证程序。但是当我运行测试失败时,似乎没有触发针对“ TBC”值设置验证器的规则。
律师验证器
public FluentSolicitorValidator()
{
RuleFor(s => s.InstructionId).GreaterThan(0)
.WithMessage("Instruction id must be positive");
RuleFor(s => s.CompanyName).NotEmpty().MaximumLength(32).Matches("^[a-zA-Z0-9 ]*$") //Not empty string, alphanumeric, allows spaces
.WithMessage("Company name must be alphanumerical only.");
RuleFor(s => s.SolicitorReference).MaximumLength(16);
RuleFor(s => s.SolicitorAddress).NotNull();
RuleFor(s => s.SolicitorAddress).SetValidator(new FluentAddressValidator()).When(s => s.SolicitorAddress.HouseNum != "TBC" &&
s.SolicitorAddress.HouseName != "TBC" &&
s.SolicitorAddress.StreetName != "TBC" &&
s.SolicitorAddress.Locality != "TBC" &&
s.SolicitorAddress.Town != "TBC" &&
s.SolicitorAddress.County != "TBC" &&
s.SolicitorAddress.Postcode != "TBC");
}
律师类
public partial class Solicitor
{
private int _solicitorId;
private int _instructionId;
private string _companyName;
private string _solicitorReference;
private int _solicitorAddressId;
public Solicitor()
{
CompanyName = "TBC";
SolicitorReference = "";
SolicitorAddress = generateTBCAddress();
}
[Key]
public int SolicitorId
{
get => _solicitorId;
set => _solicitorId = Math.Abs(value);
}
public int InstructionId
{
get => _instructionId;
set => _instructionId = Math.Abs(value);
}
public int SolicitorAddressId
{
get => _solicitorAddressId;
set => _solicitorAddressId = Math.Abs(value);
}
[StringLength(32)]
public string CompanyName
{
get => _companyName;
set => _companyName = value.Trim();
}
[StringLength(16)]
public string SolicitorReference
{
get => _solicitorReference;
set => _solicitorReference = value.Trim();
}
public virtual Address SolicitorAddress { get; set; }
public virtual SolicitorInstruction SolicitorInstruction { get; set; }
private Address generateTBCAddress()
{
Address address = new Address
{
HouseNum = "TBC",
HouseName = "TBC",
StreetName = "TBC",
Locality = "TBC",
Town = "TBC",
County = "TBC",
Postcode = "TBC"
};
return address;
}
律师验证人测试
public void ValidatorDisallowsAnyOtherAddressValuesThatAreNotValidOrTBC()
{
Address tbdAddress = new Address
{
HouseNum = "TBD",
HouseName = "TBD",
StreetName = "TBD",
Locality = "TBD",
Town = "TBD",
County = "TBD",
Postcode = "TBD"
};
Solicitor soli = new Solicitor
{
SolicitorAddress = tbdAddress
};
_validator.ShouldHaveValidationErrorFor(s => s.SolicitorAddress, soli); //A validation error should be produced here as the "TBD" values for address should trigger the address validator as they are not valid address values nor "TBC"
}
地址验证器
public FluentAddressValidator()
{
RuleFor(a => a.HouseNum).NotEmpty().MaximumLength(8).Matches(RegexStrings.alphanumericalSpaceCaseInsensitiveRegex) //Not empty string, alphanumeric, allows spaces
.WithMessage("House number must be alphanumerical only.");
RuleFor(a => a.HouseName).NotEmpty().MaximumLength(32).Matches(RegexStrings.alphabeticalSpaceCaseInsensitiveRegex) //Not empty string, alphabetical, includes spaces
.WithMessage("House name must be alphabetical only.");
RuleFor(a => a.StreetName).NotEmpty().MaximumLength(32).Matches(RegexStrings.alphabeticalSpaceCaseInsensitiveRegex) //Not empty string, alphabetical, includes spaces
.WithMessage("Street name must be alphabetical only.");
RuleFor(a => a.Locality).NotEmpty().MaximumLength(32).Matches(RegexStrings.alphabeticalSpaceCaseInsensitiveRegex) //Not empty string, alphabetical, includes spaces
.WithMessage("Locality must be alphabetical only.");
RuleFor(a => a.Town).NotEmpty().MaximumLength(32).Matches(RegexStrings.alphabeticalSpaceCaseInsensitiveRegex) //Not empty string, alphabetical, includes spaces
.WithMessage("Town must be alphabetical only.");
RuleFor(a => a.County).NotEmpty().MaximumLength(32).Matches(RegexStrings.alphabeticalSpaceCaseInsensitiveRegex) //Not empty string, alphabetical, includes spaces
.WithMessage("County must be alphabetical only.");
RuleFor(a => a.Postcode).NotEmpty().MaximumLength(16).Must(Common.IsValidUkPostcode) //Not empty string, is valid uk postcode
.WithMessage("Postcode must follow valid UK postcode format.");
}