public void ValidateMultipleRegexFields() { var obj = new ModelWithMultipleValidationAttributes() { RegexField = 4567, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); obj = new ModelWithMultipleValidationAttributes() { RegexField = 456, }; validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); obj = new ModelWithMultipleValidationAttributes() { RegexField = -23, }; validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidateRegexWithAllowNullSetToFalse() { var obj = new ModelWithOneNullableRegexIntField() { RegexField = null, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); obj = new ModelWithOneNullableRegexIntField() { RegexField = 2, }; validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); obj = new ModelWithOneNullableRegexIntField() { RegexField = -2 }; validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidateEmptyCollectionAllowedField_ShouldPassIfEmptyCollectIsProvided() { var obj = new ModelWithRequiredCollection(); var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ValidateEmptyCollectionAllowedField_ShouldNotPassIfNullCollectIsProvided() { var obj = new ModelWithRequiredCollection(); obj.List = null; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ShouldThrowBecauseOfPropertyValueNotBeingSameTypeAsValue() { var conditional = new ModelWithIncorrectEnumDependentPropertyType() { TestEnum = TestEnum.One, RegexField = 2 }; Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(conditional)); }
public void ShouldThrowBecauseOfPropertyValueNotBeingABool() { var invalid = new ModelWithIncorrectDependentPropertyType { Enabled = 2, RegexField = 20 }; Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid)); }
public void ShouldThrowBecauseOfNonExistingPropertyValue() { var invalid = new ModelWithIncorrectPropertyValue { Enabled = true, RegexField = 20 }; Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid)); }
public void ValidatePositiveIntegerWithNegativeValue() { var obj = new ModelWithOneRegexIntField { RegexField = -20 }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidatePositiveIntegerWithNullValue() { var obj = new ModelWithOneNullableRegexIntField() { RegexField = null }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidatePositiveNumberWithNineValues() { var obj = new ModelWithPositiveNumberWithNoMoreThanNineValues() { RegexField = 123456789 }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ValidatePositiveNumberWithMoreThanNineValues() { var obj = new ModelWithPositiveNumberWithNoMoreThanNineValues() { RegexField = int.MaxValue }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidateDefaultValueNotRequiredField_ShouldPassIfDefaultValueIsProvided() { var obj = new ModelWithRequiredDefaultValueAllowed() { Value = 0 }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ValidateNullRequiredField() { var obj = new ModelWithOneRequiredStringField { RequiredField = null }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidatePopulatedRequiredField() { var obj = new ModelWithOneRequiredStringField { RequiredField = "required" }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ShouldPassValidationIfConditionalEnumConditionsIsMetAndValueIsCorrect() { var conditional = new ModelWithConditionalEnumValue { TestEnum = TestEnum.One, RegexField = 2 }; var result = ModelValidator.ValidateEntity(conditional); Assert.IsFalse(result.HasError); }
public void ShouldPassValidationIfValueIsNotEmptyString() { var obj = new ModelWithConditionalRequiredNotEmptyString { Enabled = true, Value = " d ", }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void HandlesInheritedAttributes() { var obj = new ModelWithInheritedAttributes(); var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); obj.Guid = Guid.NewGuid(); validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ShouldPassValidationIfConditionIsMetAndValueIsNotNull() { var obj = new ModelWithConditionalRequiredGuid() { Enabled = true, RequiredField = Guid.Empty, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ShouldPassValidationIfConditionIsMetAndValueIsNotDefault() { var obj = new ModelWithConditionalRequiredNotDefaultGuid() { Enabled = true, Value = Guid.NewGuid() }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ShouldNotValidateIfConditionNoMet() { var obj = new ModelWithConditionalRequiredGuid() { Enabled = false, RequiredField = null, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); }
public void ValidateEmptyCollectionNotAllowedField_ShouldNotPassIfEmptyCollectIsProvided() { var obj = new ModelWithRequiredNonEmptyCollection(); obj.Array = Enumerable.Empty <string>().ToArray(); obj.List = obj.Array.ToList(); obj.Collection = obj.Array.ToHashSet(); obj.IEnumerable = obj.Array.ToDictionary(x => x); var validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); Assert.AreEqual(4, validation.Errors.Count); }
public void ValidateRegexWithAllowNullSetToTrue() { var obj = new ModelWithRegExAndAllowNull() { RegexField = null, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); obj = new ModelWithRegExAndAllowNull() { RegexField = int.MaxValue, }; validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidateOnlyNumbersOrNothing() { var obj = new ModelWithOnlyDigits() { RegexField = 23, }; var validation = ModelValidator.ValidateEntity(obj); Assert.IsFalse(validation.HasError); obj = new ModelWithOnlyDigits() { RegexField = null, }; validation = ModelValidator.ValidateEntity(obj); Assert.IsTrue(validation.HasError); }
public void ValidateInvalidRegexWithDependentStringProperty() { var notEnabled = new ModelWithPositiveNumberAndDependentStringProperty() { RegexField = -40, Enabled = "false" }; var enabled = new ModelWithPositiveNumberAndDependentStringProperty() { RegexField = -40, Enabled = "true" }; var validationNotEnabled = ModelValidator.ValidateEntity(notEnabled); var validationEnabled = ModelValidator.ValidateEntity(enabled); Assert.IsFalse(validationNotEnabled.HasError); Assert.IsTrue(validationEnabled.HasError); }
public void ValidateValidRegexWithDependentBoolProperty() { var notEnabled = new ModelWithPositiveNumberAndDependentBoolProperty() { RegexField = 40, Enabled = false }; var enabled = new ModelWithPositiveNumberAndDependentBoolProperty() { RegexField = 40, Enabled = true }; var validationNotEnabled = ModelValidator.ValidateEntity(notEnabled); var validationEnabled = ModelValidator.ValidateEntity(enabled); Assert.IsFalse(validationNotEnabled.HasError); Assert.IsFalse(validationEnabled.HasError); }
public void ShouldThrowBecauseOfNonExistingPropertyName() { var invalid = new ModelWithInvalidDependentProperty(); Assert.ThrowsException <ValidationException>(() => ModelValidator.ValidateEntity(invalid)); }