private static void RegisterValidators(DefaultValidatorProvider validatorProvider) { validatorProvider.RegisterValidator(new SaveAddressValidator()); validatorProvider.RegisterValidator(new SaveContactInfoValidator()); validatorProvider.RegisterValidator(new SaveEmployeeValidator()); validatorProvider.RegisterValidator(new SaveManagerValidator()); }
public void Validate() { // Arrange var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveEmployeeValidator()); Validator.SetValidatorProvider(validatorProvider); var manager = new Manager(); // Act var validator = new AncestorTypeValidator<Manager, Employee>(RulesSets.Crud.Save); var results = validator.Validate(manager); // Assert Assert.That(results, Is.Not.Empty); }
public void GetValidators() { // Arrange var mockValidator = Substitute.For<IValidator<object>>(); var provider = new DefaultValidatorProvider(); provider.RegisterValidator(mockValidator); // Act var validators = provider.GetValidators<object>(); // Assert Assert.That(validators, Is.Not.Empty); Assert.That(validators, Has.Member(mockValidator)); }
private static void RegisterValidators(DefaultValidatorProvider validatorProvider) { validatorProvider.RegisterValidator(new SaveLoanApplicationValidator()); validatorProvider.RegisterValidator(new SubmitLoanApplicationValidator()); }
public void Validate_NonGeneric_SubType() { // Arrange var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveEmployeeValidator()); Validator.SetValidatorProvider(validatorProvider); // Act var results = Validator.Validate(typeof(Employee), new Manager(), RulesSets.Crud.Save); // Assert Assert.That(results, Is.Not.Empty); }
public void When_appliesTo_is_false_then_validator_is_not_executed() { // Arrange var mockValidator = Substitute.For<IValidator<object>>(); mockValidator.AppliesTo("").Returns(false); var provider = new DefaultValidatorProvider(); provider.RegisterValidator(mockValidator); Validator.SetValidatorProvider(provider); // Act var objectToValidate = new object(); Validator.Validate(objectToValidate); // Assert mockValidator.DidNotReceive().Validate(objectToValidate); }
public void If_WhenPredicateFalse_ShouldNotValidate() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var acceptableLine1 = "This is a test of the emergency broadcast system."; var validator = Properties<Employee> .For(e => e.Address) .Cascade("Save") .If(e => e.Address.Line1 != acceptableLine1) ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = new Address() { Line1 = acceptableLine1, } }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Empty); }
public void If_WhenPredicateTrue_ShouldValidate() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var acceptableLine1 = "This is a test of the emergency broadcast system."; var validator = Properties<Employee> .For(e => e.Address) .Cascade("Save") .If(e => e.Address.Line1 != acceptableLine1) ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = new Address() { Line1 = null, } }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Not.Empty); var result = results.First(); Assert.That(result.Context, Is.EqualTo(employee)); Assert.That(result.PropertyName, Is.EqualTo("Address.Line1")); Assert.That(result.Severity, Is.EqualTo(ValidationResultSeverity.Error)); Assert.That(result.Type, Is.EqualTo(null)); }
public void Cascade_WithIncompatibleType() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var validator = Properties<Employee> .For(e => e.Address) .Required(); // Act Assert.Throws<ArgumentOutOfRangeException>(() => validator.Cascade<ContactInfo>("Save")); }
public void CascadeUsingType() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var message = "This is a test failure."; var validator = Properties<Employee> .For(e => e.Address) .Required() .Cascade<object>("Save") .Message(message) ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = new Address() { Line1 = null, } }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Empty); }
public void WhenCascading_Message_DoesNotOverrideMessagesFromPropertyValidation() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var message = "This is a test failure."; var validator = Properties<Employee> .For(e => e.Address) .Required() .Cascade("Save") .Message(message) ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = new Address() { Line1 = null, } }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Not.Empty); var result = results.First(vr => vr.PropertyName == "Address.Line1"); Assert.That(result.Message, Is.Not.EqualTo(message)); }
public void WhenNotRequired_AndCascading_AndPropertyIsEmpty_ShouldSucceed() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var validator = Properties<Employee> .For(e => e.Address) .NotRequired() .Cascade("Save") ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = null }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Empty); }
public void CascadeRequiredProperty() { var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveAddressValidator()); Validator.SetValidatorProvider(validatorProvider); var validator = Properties<Employee> .For(e => e.Address) .Required() .Cascade("Save") ; // Act var employee = new Employee() { ReportsTo = new Manager(), Address = new Address() { Line1 = null, } }; var results = validator.Validate(employee); // Assert Assert.That(results, Is.Not.Empty); var result = results.First(); Assert.That(result.Context, Is.EqualTo(employee)); Assert.That(result.PropertyName, Is.EqualTo("Address.Line1")); Assert.That(result.Severity, Is.EqualTo(ValidationResultSeverity.Error)); Assert.That(result.Type, Is.EqualTo(null)); }
public void Cascade_WithGenericType_InValidValues() { // Arrange var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveEmployeeValidator()); validatorProvider.RegisterValidator(new SaveManagerValidator()); Validator.SetValidatorProvider(validatorProvider); var validator = Properties<Manager> .For(e => e.Reports) .Cascade<Employee>("Save") ; // Act var manager = new Manager(); var reports = Builder<Manager> .CreateListOfSize(1) .All() .Do(m => m.Age += 20) .Do(m => m.Address = Builder<Address>.CreateNew().Build()) .Do(m => m.ContactInfo = Builder<ContactInfo>.CreateListOfSize(3).Build()) .Do(m => m.ReportsTo = null) .Build(); manager.Reports = reports.Cast<Employee>().ToList(); var results = validator.Validate(manager).ToList(); // Assert Assert.That(results, Is.Not.Empty); for (var i = 0; i < manager.Reports.Count; i++) { var expectedPropertyName = string.Format("Reports[{0}].ReportsTo", i); var expectedResults = results.Where(vr => vr.PropertyName == expectedPropertyName).ToList(); Assert.That(expectedResults, Is.Not.Empty); foreach (var expectedResult in expectedResults) Assert.That(expectedResult.Context == manager); } }
public void Cascade_WithGenericType_InvalidValuesForSubType() { // Arrange var validatorProvider = new DefaultValidatorProvider(); validatorProvider.RegisterValidator(new SaveEmployeeValidator()); validatorProvider.RegisterValidator(new SaveManagerValidator()); Validator.SetValidatorProvider(validatorProvider); var validator = Properties<Manager> .For(e => e.Reports) .Cascade<Employee>("Save") ; // Act var manager = new Manager(); var reports = Builder<Manager> .CreateListOfSize(1) .All() .Do(m => m.Age += 20) .Do(m => m.Address = Builder<Address>.CreateNew().Build()) .Do(m => m.ContactInfo = Builder<ContactInfo>.CreateListOfSize(3).Build()) .Do(m => m.ReportsTo = manager) .Do(m => m.IsSalaried = true) .Do(m => m.Reports = new List<Employee>()) .Build(); manager.Reports = reports.Cast<Employee>().ToList(); var results = validator.Validate(manager).ToList(); // Assert Assert.That(results, Is.Empty); }