public void Should_stop_validating_after_first_error() { var validator = new EntityValidator(new ActivatorServiceProvider(), null); var entity = new Validatable(); var results = validator.Validate(entity, stopOnFirstError: true); Assert.That(results.Count(), Is.EqualTo(1)); var result = results.First(); Assert.That(result.ErrorMessage, Is.EqualTo("The NotNullProperty field is required.")); Assert.That(result.MemberNames.Count(), Is.EqualTo(1)); Assert.That(result.MemberNames.First(), Is.EqualTo("NotNullProperty")); }
public void Should_validate_all_properties_and_fields() { var validator = new EntityValidator(new ActivatorServiceProvider(), null); var entity = new Validatable(); var results = validator.Validate(entity); Assert.That(results.Count(), Is.EqualTo(2)); var result = results.First(); Assert.That(result.ErrorMessage, Is.EqualTo("The NotNullProperty field is required.")); Assert.That(result.MemberNames.Count(), Is.EqualTo(1)); Assert.That(result.MemberNames.First(), Is.EqualTo("NotNullProperty")); result = results.Last(); Assert.That(result.ErrorMessage, Is.EqualTo("The notNullField field is required.")); Assert.That(result.MemberNames.Count(), Is.EqualTo(1)); Assert.That(result.MemberNames.First(), Is.EqualTo("notNullField")); }
public void IsValidatedFail() { try { var validatable = new Validatable(); FulcrumAssert.IsValidated(validatable, CodeLocation.AsString()); UT.Assert.Fail("An exception should have been thrown"); } catch (FulcrumAssertionFailedException fulcrumException) { UT.Assert.IsNotNull(fulcrumException.TechnicalMessage); UT.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith("Expected validation to pass (Property Validatable.Name"), $"TechnicalMessage: '{fulcrumException.TechnicalMessage}'"); } catch (Exception e) { UT.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}."); } }
public void IsValidatedFail() { try { var validatable = new Validatable(); InternalContract.RequireValidated(validatable, nameof(validatable)); Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail("An exception should have been thrown"); } catch (FulcrumContractException fulcrumException) { Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(fulcrumException?.TechnicalMessage); Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith("Validation failed")); Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(fulcrumException.TechnicalMessage.Contains("Property Name")); } catch (Exception e) { Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}."); } }
public void MainTest() { using (var session = Domain.OpenSession()) { using (var transactionScope = session.OpenTransaction()) { Validatable first = null; Validatable second; AssertEx.Throws <ValidationFailedException>(() => { first = new Validatable(); session.Validate(); }); first.IsValid = true; session.Validate(); transactionScope.Complete(); } } }
public async Task When_Value_Is_Greater_Than_PropertyToCheck_Value_Then_Valid() { // Arrange var validatable = new Validatable { PropertyToCheckAgainst = 1, MustBeGreaterThanProperty = 2 }; var validator = new BlueprintValidator(new IValidationSource[] { new DataAnnotationsValidationSource() }); // Act var failures = await validator.GetValidationResultsAsync(validatable, null); var failedProperties = failures.AsDictionary().Keys; // Assert CollectionAssert.DoesNotContain(failedProperties, "MustBeGreaterThanProperty"); }
public void InvalidCommitTest() { using (var session = Domain.OpenSession()) { var transactionScope = session.OpenTransaction(); var first = new Validatable { IsValid = true }; var second = new Validatable { IsValid = false }; var third = new Validatable { IsValid = true }; transactionScope.Complete(); AssertEx.Throws <ValidationFailedException>(transactionScope.Dispose); } }
public void EnforceValidationTest() { using (var session = Domain.OpenSession()) { var transactionScope = session.OpenTransaction(); var entity = new Validatable { IsValid = false }; AssertEx.Throws <ValidationFailedException>(session.Validate); AssertEx.Throws <ValidationFailedException>(session.Validate); entity.IsValid = true; session.Validate(); entity.IsValid = false; AssertEx.Throws <ValidationFailedException>(session.Validate); transactionScope.Complete(); AssertEx.Throws <ValidationFailedException>(transactionScope.Dispose); } }
public async Task When_Value_Is_Equal_To_PropertyToCheck_Value_Then_InValid() { var date = DateTime.Now; // Arrange var validatable = new Validatable { PropertyToCheckAgainst = date, MustBeGreaterThanProperty = date }; var validator = new BlueprintValidator(new IValidationSource[] { new DataAnnotationsValidationSource() }); // Act var failures = await validator.GetValidationResultsAsync(validatable, null); var failedProperties = failures.AsDictionary().Keys; // Assert CollectionAssert.Contains(failedProperties, "MustBeGreaterThanProperty"); }
public void IsValidatedFail() { try { var validatable = new Validatable(); InternalContract.RequireValidated(validatable, nameof(validatable)); UT.Assert.Fail("An exception should have been thrown"); } catch (FulcrumContractException fulcrumException) { UT.Assert.IsNotNull(fulcrumException.TechnicalMessage); var validationFailed = "ContractViolation: Validation failed"; UT.Assert.IsTrue(fulcrumException.TechnicalMessage.StartsWith(validationFailed), $"Expected {nameof(fulcrumException.TechnicalMessage)} to start with \"{validationFailed}\", but the message was \"{fulcrumException.TechnicalMessage}\"."); UT.Assert.IsTrue(fulcrumException.TechnicalMessage.Contains("Property Validatable.Name"), $"TechnicalMessage: '{fulcrumException.TechnicalMessage}'"); } catch (Exception e) { UT.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}."); } }
public UserValidator() { LastName = new Validatable <string>(); Name = new Validatable <string>(); Email = new Validatable <string>(); _unit1 = new ValidationUnit(Name, LastName, Email); // Name validations Name.Validations.Add(new IsNotNullOrEmptyRule <string> { ValidationMessage = "A name is required." }); //Lastname validations LastName.Validations.Add(new IsNotNullOrEmptyRule <string> { ValidationMessage = "A lastname is required." }); //Email validations Email.Validations.Add(new IsNotNullOrEmptyRule <string> { ValidationMessage = "A email is required." }); Email.Validations.Add(new EmailRule()); }
/// <summary> /// Validate the data annotations /// </summary> /// <param name="validationResults">out parameter for the result of the validation</param> public static bool Validate(this Validatable obj, ICollection <ValidationResult> validationResults) { var validationContext = new ValidationContext(obj); return(Validator.TryValidateObject(obj, validationContext, validationResults, true)); }
public static Validatable <TModel> Must <TModel>(this Validatable <TModel> validatable, Func <TModel, bool> predicate, string errorMessage = "") { validatable.Validations.Add(new FunctionRule <TModel>(predicate).WithMessage(errorMessage)); return(validatable); }
public static Validatable <TModel> WithRule <TModel>(this Validatable <TModel> validatable, params IValidationRule <TModel>[] validations) { validatable.Validations.AddRange(validations); return(validatable); }
public WhenRule(ref Validatable <T> context, Func <T, bool> ruleFunc) { _context = context; _ruleFunc = ruleFunc; }
public static Validatable <TModel> When <TModel>(this Validatable <TModel> validatable, Func <TModel, bool> predicate) { validatable.HasWhenCondition(true); validatable.Validations.Insert(0, new WhenRule <TModel>(ref validatable, predicate)); return(validatable); }
/// <summary> /// Validate the data annotations /// </summary> public static bool Validate(this Validatable obj) { return(obj.Validate(new List <ValidationResult>())); }