public void DefaultErrorMessage_InclusiveBetweenValidator() { var validator = new InclusiveBetweenValidator(1, 3); Assert.That(validator.ErrorMessageSource, Is.TypeOf(typeof(LocalizedStringSource))); Assert.That(validator.ErrorMessageSource.GetString(), Is.EqualTo(Messages.inclusivebetween_error)); }
public void To_and_from_properties_should_be_set() { var validator = new InclusiveBetweenValidator(1, 10); validator.From.ShouldEqual(1); validator.To.ShouldEqual(10); }
public void To_and_from_properties_should_be_set_for_strings() { var validator = new InclusiveBetweenValidator("a", "c"); validator.From.ShouldEqual("a"); validator.To.ShouldEqual("c"); }
public void Format_IBetweenValidators() { var validator1 = new ExclusiveBetweenValidator(3, 8); var validator2 = new InclusiveBetweenValidator(5, 9); Assert.That(_formatter.Format(validator1, _typeNameFormatter), Is.EqualTo("ExclusiveBetweenValidator { From = '3', To = '8' }")); Assert.That(_formatter.Format(validator2, _typeNameFormatter), Is.EqualTo("InclusiveBetweenValidator { From = '5', To = '9' }")); }
public void Should_create_inclusivebetweenadapter_for_inclusivebetweenvalidator() { // Given var validator = new InclusiveBetweenValidator(1, 10); // When var result = factory.Create(this.rule, validator); // Then result.ShouldBeOfType <InclusiveBetweenAdapter>(); }
public void When_the_text_is_larger_than_the_range_then_the_validator_should_fail_for_dates() { DateTime value = new DateTime(2010, 1, 1); var validator = new InclusiveBetweenValidator(fromDate, toDate); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeFalse(); }
public void To_and_from_properties_should_be_set_for_dates() { var validator = new InclusiveBetweenValidator(fromDate, toDate); validator.From.ShouldEqual(fromDate); validator.To.ShouldEqual(toDate); }
public void When_the_text_is_larger_than_the_range_then_the_validator_should_fail() { int value = 11; var validator = new InclusiveBetweenValidator(1, 10); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeFalse(); }
public void When_the_validator_fails_the_error_message_should_be_set_for_doubles() { var validator = new InclusiveBetweenValidator(1.2, 10.9); var result = validator.Validate(new PropertyValidatorContext("Value", null, x => 0.0)); result.Single().ErrorMessage.ShouldEqual("'Value' must be between 1.2 and 10.9. You entered 0."); }
public void When_the_value_is_smaller_than_the_range_then_the_validator_should_fail_for_strings() { string value = "aaa"; var validator = new InclusiveBetweenValidator("bbb", "zz"); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeFalse(); }
private void AddAttributeValidation() { var type = typeof(TModel); var ignoreAttrsAttr = type.GetTypeInfo().GetCustomAttributes(typeof(IgnoreValidationAttributesAttribute), true) .FirstOrDefault() as IgnoreValidationAttributesAttribute; var ignoreProps = ignoreAttrsAttr != null ? new HashSet <string>(ignoreAttrsAttr.Properties ?? new string[0]) : new HashSet <string>(); foreach (var prop in type.GetProperties().Where(o => !ignoreProps.Contains(o.Name))) { var attrs = prop.GetCustomAttributes(true); //Add validation from attributes foreach (var attr in attrs.OfType <ValidationAttribute>()) { IPropertyValidator propValidator = null; #region NotNullAttribute if (attr is NotNullAttribute) { propValidator = new NotNullValidator(); } #endregion #region EmailAttribute if (attr is EmailAttribute) { propValidator = new EmailValidator(); } #endregion #region EnumAttribute var enumAttr = attr as EnumAttribute; if (enumAttr != null) { propValidator = new EnumValidator(enumAttr.Type); } #endregion #region NotEmptyAttribute var notEmptyAttr = attr as NotEmptyAttribute; if (notEmptyAttr != null) { propValidator = new NotEmptyValidator(notEmptyAttr.DefaultValue ?? prop.PropertyType.GetDefaultValue()); } #endregion #region NotNullOrEmptyAttribute var notNullOrEmptyAttr = attr as NotNullOrEmptyAttribute; if (notNullOrEmptyAttr != null) { propValidator = new NotEmptyValidator(prop.PropertyType.GetDefaultValue()); AddAttributePropertyValidator(new NotNullValidator(), prop, attr.IncludePropertyName); } #endregion #region EqualAttribute AddComparisonValidator(attr as EqualAttribute, type, prop, o => new EqualValidator(o), (func, info) => new EqualValidator(func, info)); #endregion #region LengthAttribute var lengthAttr = attr as LengthAttribute; if (lengthAttr != null) { propValidator = new LengthValidator(lengthAttr.Min, lengthAttr.Max); } #endregion #region NotEqualAttribute AddComparisonValidator(attr as NotEqualAttribute, type, prop, o => new NotEqualValidator(o), (func, info) => new NotEqualValidator(func, info)); #endregion #region RegularExpressionAttribute var regexAttr = attr as RegularExpressionAttribute; if (regexAttr != null) { propValidator = new RegularExpressionValidator(regexAttr.Expression, regexAttr.RegexOptions); } #endregion #region CreditCardAttribute if (attr is CreditCardAttribute) { propValidator = new CreditCardValidator(); } #endregion #region ScalePrecisionAttribute var scalePrecisionAttr = attr as ScalePrecisionAttribute; if (scalePrecisionAttr != null) { propValidator = new ScalePrecisionValidator(scalePrecisionAttr.Scale, scalePrecisionAttr.Precision); } #endregion #region ExactLengthAttribute var exctLenAttr = attr as ExactLengthAttribute; if (exctLenAttr != null) { propValidator = new ExactLengthValidator(exctLenAttr.Length); } #endregion #region ExclusiveBetweenAttribute var exclusiveBetweenAttribute = attr as ExclusiveBetweenAttribute; if (exclusiveBetweenAttribute != null) { if (exclusiveBetweenAttribute.From != null && exclusiveBetweenAttribute.To != null && exclusiveBetweenAttribute.From.GetType() != exclusiveBetweenAttribute.To.GetType()) { var fromConverted = Convert.ChangeType(exclusiveBetweenAttribute.From, exclusiveBetweenAttribute.To.GetType()); propValidator = new ExclusiveBetweenValidator(fromConverted as IComparable, exclusiveBetweenAttribute.To as IComparable); } else { propValidator = new ExclusiveBetweenValidator(exclusiveBetweenAttribute.From as IComparable, exclusiveBetweenAttribute.To as IComparable); } } #endregion #region ExclusiveBetweenAttribute var inclusiveBetweenAttribute = attr as InclusiveBetweenAttribute; if (inclusiveBetweenAttribute != null) { if (inclusiveBetweenAttribute.From != null && inclusiveBetweenAttribute.To != null && inclusiveBetweenAttribute.From.GetType() != inclusiveBetweenAttribute.To.GetType()) { var fromConverted = Convert.ChangeType(inclusiveBetweenAttribute.From, inclusiveBetweenAttribute.To.GetType()); propValidator = new InclusiveBetweenValidator(fromConverted as IComparable, inclusiveBetweenAttribute.To as IComparable); } else { propValidator = new InclusiveBetweenValidator(inclusiveBetweenAttribute.From as IComparable, inclusiveBetweenAttribute.To as IComparable); } } #endregion #region GreaterThanAttribute AddComparisonValidator(attr as GreaterThanAttribute, type, prop, o => new GreaterThanValidator(o as IComparable), (func, info) => new GreaterThanValidator(func, info)); #endregion #region GreaterThanOrEqualAttribute AddComparisonValidator(attr as GreaterThanOrEqualAttribute, type, prop, o => new GreaterThanOrEqualValidator(o as IComparable), (func, info) => new GreaterThanOrEqualValidator(func, info)); #endregion #region LessThanOrEqualAttribute AddComparisonValidator(attr as LessThanOrEqualAttribute, type, prop, o => new LessThanOrEqualValidator(o as IComparable), (func, info) => new LessThanOrEqualValidator(func, info)); #endregion #region LessThanAttribute AddComparisonValidator(attr as LessThanAttribute, type, prop, o => new LessThanValidator(o as IComparable), (func, info) => new LessThanValidator(func, info)); #endregion if (propValidator == null) { continue; } AddAttributePropertyValidator(propValidator, prop, attr.IncludePropertyName); } } }
public void Should_create_inclusivebetweenadapter_for_inclusivebetweenvalidator() { // Given var validator = new InclusiveBetweenValidator(1, 10); // When var result = factory.Create(this.rule, validator); // Then result.ShouldBeOfType<InclusiveBetweenAdapter>(); }
public void When_the_value_is_between_the_range_specified_then_the_validator_should_pass_for_strings() { string value = "bbb"; var validator = new InclusiveBetweenValidator("aa", "zz"); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_value_is_exactly_the_size_of_the_upper_bound_then_the_validator_should_pass_for_dates() { DateTime value = new DateTime(2009, 12, 31); var validator = new InclusiveBetweenValidator(fromDate, toDate); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_value_is_between_the_range_specified_then_the_validator_should_pass_for_doubles() { double value = 5.0; var validator = new InclusiveBetweenValidator(1.0, 10.0); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_value_is_between_the_range_specified_then_the_validator_should_pass_for_dates() { DateTime value = new DateTime(2009, 9, 9); var validator = new InclusiveBetweenValidator(fromDate, toDate); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_validator_fails_the_error_message_should_be_set_for_strings() { string value = "aaa"; var validator = new InclusiveBetweenValidator("bbb", "zzz"); var result = validator.Validate(new PropertyValidatorContext("Value", null, x => value)); result.Single().ErrorMessage.ShouldEqual("'Value' must be between bbb and zzz. You entered aaa."); }
public void When_the_value_is_exactly_the_size_of_the_upper_bound_then_the_validator_should_pass_for_strings() { string value = "aa"; var validator = new InclusiveBetweenValidator("aa", "zz"); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_value_is_smaller_than_the_range_then_the_validator_should_fail_for_doubles() { double value = 0.9; var validator = new InclusiveBetweenValidator(1.0, 10.0); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeFalse(); }
public void When_the_value_is_exactly_the_size_of_the_upper_bound_then_the_validator_should_pass_for_doubles() { double value = 10.0; var validator = new InclusiveBetweenValidator(1.0, 10.0); var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => value)); result.IsValid().ShouldBeTrue(); }
public void When_the_validator_fails_the_error_message_should_be_set_for_dates() { DateTime value = new DateTime(2008, 1, 1); var validator = new InclusiveBetweenValidator(fromDate, toDate); var result = validator.Validate(new PropertyValidatorContext("Value", null, x => value)); result.Single().ErrorMessage.ShouldEqual("'Value' must be between 1/1/2009 12:00:00 AM and 12/31/2009 12:00:00 AM. You entered 1/1/2008 12:00:00 AM."); }