public LocalizedRegularExpressionAttribute(RegularExpressionAttribute attribute, Localizer t)
            : base(attribute.Pattern) {
            if ( !String.IsNullOrEmpty(attribute.ErrorMessage) )
                ErrorMessage = attribute.ErrorMessage;

            T = t;
        }
        public static void Can_set_and_get_MatchTimeout()
        {
            var attribute = new RegularExpressionAttribute("SomePattern");

            attribute.MatchTimeoutInMilliseconds = 12345;
            Assert.Equal(12345, attribute.MatchTimeoutInMilliseconds);
        }
        public void IsCultureCodeValidTests()
        {
            var attribute = new RegularExpressionAttribute(Expressions.CultureCode);

            Assert.IsTrue(attribute.IsValid(null));
            Assert.IsTrue(attribute.IsValid(string.Empty));
            Assert.IsTrue(attribute.IsValid("ab"));
            Assert.IsTrue(attribute.IsValid("XYZ"));
            Assert.IsTrue(attribute.IsValid("ab-WxYz"));
            Assert.IsTrue(attribute.IsValid("abc-wXyZ"));
            Assert.IsTrue(attribute.IsValid("ab-cd"));
            Assert.IsTrue(attribute.IsValid("UVW-XYZ"));
            Assert.IsTrue(attribute.IsValid("ab-123"));
            Assert.IsTrue(attribute.IsValid("XYZ-789"));
            Assert.IsTrue(attribute.IsValid("ab-CD-Efgh"));
            Assert.IsTrue(attribute.IsValid("UVW-XYZ-abcd"));
            Assert.IsTrue(attribute.IsValid("ab-123-cdef"));
            Assert.IsTrue(attribute.IsValid("XYZ-789-abcd"));

            Assert.IsFalse(attribute.IsValid("12"));
            Assert.IsFalse(attribute.IsValid("789"));
            Assert.IsFalse(attribute.IsValid("6789"));
            Assert.IsFalse(attribute.IsValid("A"));
            Assert.IsFalse(attribute.IsValid("AB-"));
            Assert.IsFalse(attribute.IsValid("WXYZ"));
            Assert.IsFalse(attribute.IsValid("AB-789-XYZ"));
            Assert.IsFalse(attribute.IsValid("AB-789-VWXYZ"));
            Assert.IsFalse(attribute.IsValid("UVW-XYZ-0000"));
        }
        public static void Validate_successful_for_null_or_empty_value()
        {
            var attribute = new RegularExpressionAttribute("SomePattern");

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));         // Null is valid
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext)); // Empty string is valid
        }
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");
            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
        public PasswordValidationAttribute()
        {
            var configuration = DependencyResolver.Current.GetService<IGalleryConfigurationService>().Current;

            _regexAttribute = new RegularExpressionAttribute(configuration.UserPasswordRegex)
            {
                ErrorMessage = configuration.UserPasswordHint
            };
        }
Exemplo n.º 7
0
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");
            attribute.MatchTimeoutInMilliseconds = 5000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            attribute.MatchTimeoutInMilliseconds = 10000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
        public static void Validate_successful_for_value_matching_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            attribute.MatchTimeoutInMilliseconds = 5000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("defghi", s_testValidationContext));

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            attribute.MatchTimeoutInMilliseconds = 10000; // note: timeout is just a number much larger than we expect the test to take
            AssertEx.DoesNotThrow(() => attribute.Validate("bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy", s_testValidationContext));
        }
Exemplo n.º 10
0
 public override bool IsValid(object value)
 {
     if (value != null)
     {
         var regexValidator = new RegularExpressionAttribute(_pattern);
         if (!regexValidator.IsValid(value))
         {
             return false;
         }
     }
     return true;
 }
        public static void Validation_throws_InvalidOperationException_for_null_or_empty_pattern()
        {
            var attribute = new RegularExpressionAttribute(null);
            Assert.Null(attribute.Pattern);
            Assert.Throws<InvalidOperationException>(
                () => attribute.Validate("Does not matter - Pattern is null", s_testValidationContext));

            attribute = new RegularExpressionAttribute(string.Empty);
            AssertEx.Empty(attribute.Pattern);
            Assert.Throws<InvalidOperationException>(
                () => attribute.Validate("Does not matter - Pattern is empty", s_testValidationContext));
        }
        public static void Validation_throws_InvalidOperationException_for_null_or_empty_pattern()
        {
            var attribute = new RegularExpressionAttribute(null);

            Assert.Null(attribute.Pattern);
            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Does not matter - Pattern is null", s_testValidationContext));

            attribute = new RegularExpressionAttribute(string.Empty);
            AssertEx.Empty(attribute.Pattern);
            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Does not matter - Pattern is empty", s_testValidationContext));
        }
        public static void Validate_throws_for_values_which_do_not_match_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");
            Assert.Throws<ValidationException>(() => attribute.Validate("zyxwvu", s_testValidationContext)); // pattern does not match
            Assert.Throws<ValidationException>(() => attribute.Validate("defghijkl", s_testValidationContext)); // pattern only matches beginning of value
            Assert.Throws<ValidationException>(() => attribute.Validate("abcdefghi", s_testValidationContext)); // pattern only matches end of value
            Assert.Throws<ValidationException>(() => attribute.Validate("abcdefghijkl", s_testValidationContext)); // pattern only matches part of value

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            Assert.Throws<ValidationException>(() => attribute.Validate("aaaaa", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("zzzzz", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("b.z", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("a.y", s_testValidationContext));
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var pageUrl = value as string;
            if (!string.IsNullOrWhiteSpace(pageUrl))
            {
                var regExpAttribute = new RegularExpressionAttribute(PagesConstants.PageUrlRegularExpression);
                if (!regExpAttribute.IsValid(pageUrl))
                {
                    return new ValidationResult(errorMessage);
                }
            }

            return ValidationResult.Success;
        }
Exemplo n.º 15
0
        public static void Validate_throws_for_values_which_do_not_match_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            Assert.Throws <ValidationException>(() => attribute.Validate("zyxwvu", s_testValidationContext));       // pattern does not match
            Assert.Throws <ValidationException>(() => attribute.Validate("defghijkl", s_testValidationContext));    // pattern only matches beginning of value
            Assert.Throws <ValidationException>(() => attribute.Validate("abcdefghi", s_testValidationContext));    // pattern only matches end of value
            Assert.Throws <ValidationException>(() => attribute.Validate("abcdefghijkl", s_testValidationContext)); // pattern only matches part of value

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            Assert.Throws <ValidationException>(() => attribute.Validate("aaaaa", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("zzzzz", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("b.z", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("a.y", s_testValidationContext));
        }
        public static void Validate_throws_for_values_which_do_not_match_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");
            Assert.Throws<ValidationException>(() => attribute.Validate("zyxwvu", s_testValidationContext)); // pattern does not match
            Assert.Throws<ValidationException>(() => attribute.Validate("defghijkl", s_testValidationContext)); // pattern only matches beginning of value
            Assert.Throws<ValidationException>(() => attribute.Validate("abcdefghi", s_testValidationContext)); // pattern only matches end of value
            Assert.Throws<ValidationException>(() => attribute.Validate("abcdefghijkl", s_testValidationContext)); // pattern only matches part of value

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            attribute.MatchTimeoutInMilliseconds = 10000; // note: timeout is just a number much larger than we expect the test to take
            Assert.Throws<ValidationException>(() => attribute.Validate("aaaaa", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("zzzzz", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("b.z", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("a.y", s_testValidationContext));
        }
        public static void Validate_throws_for_values_which_do_not_match_pattern()
        {
            var attribute = new RegularExpressionAttribute("defghi");

            Assert.Throws <ValidationException>(() => attribute.Validate("zyxwvu", s_testValidationContext));       // pattern does not match
            Assert.Throws <ValidationException>(() => attribute.Validate("defghijkl", s_testValidationContext));    // pattern only matches beginning of value
            Assert.Throws <ValidationException>(() => attribute.Validate("abcdefghi", s_testValidationContext));    // pattern only matches end of value
            Assert.Throws <ValidationException>(() => attribute.Validate("abcdefghijkl", s_testValidationContext)); // pattern only matches part of value

            attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+");
            attribute.MatchTimeoutInMilliseconds = 10000; // note: timeout is just a number much larger than we expect the test to take
            Assert.Throws <ValidationException>(() => attribute.Validate("aaaaa", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("zzzzz", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("b.z", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("a.y", s_testValidationContext));
        }
        public void IsCurrencyCodeValidTests()
        {
            var attribute = new RegularExpressionAttribute(Expressions.CurrencyCode);

            Assert.IsTrue(attribute.IsValid(null));
            Assert.IsTrue(attribute.IsValid(string.Empty));
            Assert.IsTrue(attribute.IsValid("ABC"));
            Assert.IsTrue(attribute.IsValid("XYZ"));
            Assert.IsTrue(attribute.IsValid("123"));
            Assert.IsTrue(attribute.IsValid("789"));

            Assert.IsFalse(attribute.IsValid("AB"));
            Assert.IsFalse(attribute.IsValid("WXYZ"));
            Assert.IsFalse(attribute.IsValid("12"));
            Assert.IsFalse(attribute.IsValid("6789"));
        }
Exemplo n.º 19
0
        private ValidationAttribute CopyAttribute(ValidationAttribute attribute)
        {
            ValidationAttribute result = null;

            if (attribute is RangeAttribute)
            {
                var attr = (RangeAttribute)attribute;
                result = (attr.Minimum is double)
                    ?  new RangeAttribute((double)attr.Minimum, (double)attr.Maximum)
                    :  new RangeAttribute((int)attr.Minimum, (int)attr.Maximum);
            }

            if (attribute is RegularExpressionAttribute)
            {
                var attr = (RegularExpressionAttribute)attribute;
                result = new RegularExpressionAttribute(attr.Pattern);
            }

            if (attribute is RequiredAttribute)
                result = new RequiredAttribute();

            if (attribute is StringLengthAttribute)
            {
                var attr = (StringLengthAttribute)attribute;
                result = new StringLengthAttribute(attr.MaximumLength)
                {
                    MinimumLength = attr.MinimumLength
                };
            }

            if (attribute is DA.CompareAttribute)
            {
                var attr = (DA.CompareAttribute)attribute;
                result = new DA.CompareAttribute(attr.OtherProperty);
            }

            if (attribute is DataTypeAttribute)
            {
                var attr = (DataTypeAttribute)attribute;
                result = new DataTypeAttribute(attr.DataType);
            }

            if (result == null && attribute.GetType().GetInterfaces().Contains(typeof(ICloneable)))
                result = ((ICloneable)attribute).Clone() as ValidationAttribute;

            return result;
        }
        public void IsCuitValidTests()
        {
            var attribute = new RegularExpressionAttribute(Expressions.Cuit);

            Assert.IsTrue(attribute.IsValid(null));
            Assert.IsTrue(attribute.IsValid("20245597151"));
            Assert.IsTrue(attribute.IsValid("20-24559715-1"));
            Assert.IsTrue(attribute.IsValid("27-23840320-6"));
            Assert.IsTrue(attribute.IsValid("27238403206"));
            Assert.IsTrue(attribute.IsValid(27238403206));

            Assert.IsFalse(attribute.IsValid("20 24559715 1"));
            Assert.IsFalse(attribute.IsValid("123456789"));
            Assert.IsFalse(attribute.IsValid("aa-aaaaaaaa-a"));
            Assert.IsFalse(attribute.IsValid("4408 0412 3456 7890"));
            Assert.IsFalse(attribute.IsValid(0));
            Assert.IsFalse(attribute.IsValid(123));
        }
        public void ClientRulesWithRegexAttribute() {
            // Arrange
            var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(string), "Length");
            var context = new ControllerContext();
            var attribute = new RegularExpressionAttribute("the_pattern");
            var adapter = new RegularExpressionAttributeAdapter(metadata, context, attribute);

            // Act
            var rules = adapter.GetClientValidationRules()
                               .OrderBy(r => r.ValidationType)
                               .ToArray();

            // Assert
            Assert.AreEqual(1, rules.Length);

            Assert.AreEqual("regularExpression", rules[0].ValidationType);
            Assert.AreEqual(1, rules[0].ValidationParameters.Count);
            Assert.AreEqual("the_pattern", rules[0].ValidationParameters["pattern"]);
            Assert.AreEqual(@"The field Length must match the regular expression 'the_pattern'.", rules[0].ErrorMessage);
        }
        /// <param name="type">Type of ValidationAttribute to use.</param>
        /// <param name="id">Item ID of the Sitecore item to use for getting the error message string.</param>
        /// <param name="field">Field name of the Sitecore item to use for getting the error message string.</param>
        /// <param name="jsonProperties">Optional Json representation of properties to set on the validation attribute.</param>
        /// <param name="pattern">RegEx to be used for RegularExpressio Validations</param>
        public SitecoreValidationAttribute(Type type, string id, string field, string jsonProperties = null, string pattern = null)
            : base()
        {
            if (type == typeof(RegularExpressionAttribute))
            {
                InnerValidationAttribute = new RegularExpressionAttribute(pattern);//Activator.CreateInstance(type) as RegularExpressionAttribute;
            }
            else
            {
                InnerValidationAttribute = Activator.CreateInstance(type) as ValidationAttribute;

            }
            _type = type;
            _id = id;
            _field = field;
            _jsonProperties = jsonProperties;

            SetErrorMessage();
            if (!string.IsNullOrWhiteSpace(_jsonProperties)) SetProperties();
        }
        private ModelValidator GetValidator(ModelMetadata metadata, ControllerContext context, ValidatorData item)
        {
            DataAnnotationsModelValidationFactory factory;
            if (item.GetType().Name == "NotNullValidatorData")
            {
                ValidationAttribute attribute = new RequiredAttribute { ErrorMessage = item.MessageTemplate };
                if (AttributeFactories.TryGetValue(attribute.GetType(), out factory))
                    return factory(metadata, context, attribute);
            }
            else if (item.GetType().Name == "RegexValidatorData")
            {
                RegexValidatorData regexData = (RegexValidatorData)item;

                RegularExpressionAttribute regexAttribute = new RegularExpressionAttribute(regexData.Pattern) { ErrorMessage = item.GetMessageTemplate() };

                if (AttributeFactories.TryGetValue(regexAttribute.GetType(), out factory))
                    return factory(metadata, context, regexAttribute);
            }

            return null;
        }
Exemplo n.º 24
0
        public override ModelValidator Create(
            IStorageValidator validator, Type defaultResourceType, ModelMetadata metadata, ControllerContext context)
        {
            StorageValidator<string> vldtr = validator as StorageValidator<string>;
            if (vldtr == null)
                throw new System.IO.InvalidDataException(
                    "Validator value must be of type StorageValidator<string>.");

            //string regExp = null;// xmlElement.GetValueOrNull("regexp");
            //try
            //{
            //    regExp = validator.data.regExp;
            //}
            //catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
            //{
            //    throw new System.IO.InvalidDataException(
            //        string.Format("The regular expression was not set. Element: {0}", validator.Name));
            //}

            var attribute = new RegularExpressionAttribute(vldtr.data);
            this.BindErrorMessageToAttribte(attribute, validator, defaultResourceType);

            return new RegularExpressionAttributeAdapter(metadata, context, attribute);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalizedRegularExpressionAttributeAdapter"/> class.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The controller context.</param>
        /// <param name="attribute">The <see cref="RegularExpressionAttribute"/> attribute.</param>
        public LocalizedRegularExpressionAttributeAdapter(ModelMetadata metadata, ControllerContext context, RegularExpressionAttribute attribute) : base(metadata, context, attribute)
        {

        }
        public void CreateWithRegularExpressionAttributeRequestReturnsCorrectResult(string pattern)
        {
            // Fixture setup
            var regularExpressionAttribute = new RegularExpressionAttribute(pattern);
            var providedAttribute = new ProvidedAttribute(regularExpressionAttribute, true);
            ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute);
            var expectedRequest = new RegularExpressionRequest(regularExpressionAttribute.Pattern);
            var expectedResult = new object();
            var context = new DelegatingSpecimenContext
            {
#pragma warning disable 618
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r)
#pragma warning restore 618
            };
            var sut = new RegularExpressionAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            List<ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();
            DataAnnotationsModelValidationFactory factory;

            // Inject our new validator.
            if (metadata.ContainerType != null)
            {
                // Check if we have validation for this class name.
                if (ValidationManager.Validators.ContainsKey(metadata.ContainerType.Name))
                {
                    var validator = ValidationManager.Validators[metadata.ContainerType.Name];

                    // Check if we have validation for this property name.
                    if (validator.ContainsKey(metadata.PropertyName))
                    {
                        var property = validator[metadata.PropertyName];

                        // Only add validation to visible properties.
                        if (property.Visible)
                        {
                            // Required attribute.
                            if (property.Required)
                            {
                                ValidationAttribute required;

                                if (metadata.ModelType == typeof(bool))
                                {
                                    // For required booleans, enforce true.
                                    required = new EnforceTrueAttribute { ErrorMessage = property.ErrorMessage };
                                }
                                else if (metadata.ModelType == typeof(int) || metadata.ModelType == typeof(long) || metadata.ModelType == typeof(double) || metadata.ModelType == typeof(float))
                                {
                                    // For required int, long, double, float (dropdownlists), enforce > 0.
                                    required = new GreaterThanZeroAttribute() { ErrorMessage = property.ErrorMessage };
                                }
                                else
                                {
                                    required = new RequiredAttribute { ErrorMessage = property.ErrorMessage };
                                }

                                if (!AttributeFactories.TryGetValue(required.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return factory(metadata, context, required);
                            }

                            // Regular expression attribute.
                            if (!string.IsNullOrEmpty(property.RegularExpression))
                            {
                                RegularExpressionAttribute regEx = new RegularExpressionAttribute(property.RegularExpression) { ErrorMessage = property.ErrorMessage };

                                if (!AttributeFactories.TryGetValue(regEx.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return factory(metadata, context, regEx);
                            }

                            // Compare attribute.
                            if (!string.IsNullOrEmpty(property.Compare))
                            {
                                CompareAttribute compare = new CompareAttribute(property.Compare) { ErrorMessage = property.ErrorMessage };

                                if (!AttributeFactories.TryGetValue(compare.GetType(), out factory))
                                {
                                    factory = DefaultAttributeFactory;
                                }

                                yield return factory(metadata, context, compare);
                            }
                        }
                    }
                }
            }
        }
        public void IsPhoneNumberValidTests()
        {
            var attribute = new RegularExpressionAttribute(Expressions.PhoneNumber);

            Assert.IsTrue(attribute.IsValid(null));
            Assert.IsTrue(attribute.IsValid(string.Empty));
            Assert.IsTrue(attribute.IsValid("1234567"));
            Assert.IsTrue(attribute.IsValid("20 24559715 1"));
            Assert.IsTrue(attribute.IsValid("123456789"));
            Assert.IsTrue(attribute.IsValid("+1(234)4567789"));
            Assert.IsTrue(attribute.IsValid("+235 7789"));
            Assert.IsTrue(attribute.IsValid("00 27 238.40320"));
            Assert.IsTrue(attribute.IsValid("0011 87 9854 23"));
            Assert.IsTrue(attribute.IsValid("011 777899 854-2356"));
            Assert.IsTrue(attribute.IsValid("0011 (87) 9854 23"));
            Assert.IsTrue(attribute.IsValid("011 (777899) 854-2356"));
            Assert.IsTrue(attribute.IsValid("07778542356"));
            Assert.IsTrue(attribute.IsValid("1.234.567-0000"));
            Assert.IsTrue(attribute.IsValid("1/23/56/000"));
            Assert.IsTrue(attribute.IsValid("1/2/5/0"));
            Assert.IsTrue(attribute.IsValid("4408 0412 3456 7890"));
            Assert.IsTrue(attribute.IsValid("00"));
            Assert.IsTrue(attribute.IsValid(12));
            Assert.IsTrue(attribute.IsValid(123));
            Assert.IsTrue(attribute.IsValid(1234567890));

            Assert.IsFalse(attribute.IsValid("1/2/"));
            Assert.IsFalse(attribute.IsValid("1/23/56/000-"));
            Assert.IsFalse(attribute.IsValid("-1/23/56/000"));
            Assert.IsFalse(attribute.IsValid("a"));
            Assert.IsFalse(attribute.IsValid("0"));
            Assert.IsFalse(attribute.IsValid("+0"));
            Assert.IsFalse(attribute.IsValid("+00 27 238.40320"));
            Assert.IsFalse(attribute.IsValid("+0011 (87) 9854 23"));
            Assert.IsFalse(attribute.IsValid("+011 (777899) 854-2356"));
            Assert.IsFalse(attribute.IsValid("+07778542356"));
            Assert.IsFalse(attribute.IsValid(0));
        }
Exemplo n.º 29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RegexValidatorAdapter"/> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 /// <param name="descriptor">The descriptor.</param>
 public RegexValidatorAdapter(RegularExpressionAttribute attribute, PropertyDescriptor descriptor)
     : base("Regex", attribute, descriptor)
 {
 }
 /// <summary>
 /// Creates the validator.
 /// </summary>
 /// <param name="modelMetadata">The model metadata.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 protected override ModelValidator CreateValidatorCore(ExtendedModelMetadata modelMetadata, ControllerContext context)
 {
     var attribute = new RegularExpressionAttribute(Pattern);
     PopulateErrorMessage(attribute);
     return new RegularExpressionAttributeAdapter(modelMetadata, context, attribute);
 }
        public static void Can_construct_and_get_pattern()
        {
            var attribute = new RegularExpressionAttribute("SomePattern");

            Assert.Equal("SomePattern", attribute.Pattern);
        }
 public static void Validate_successful_for_null_or_empty_value()
 {
     var attribute = new RegularExpressionAttribute("SomePattern");
     AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
     AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext)); // Empty string is valid
 }
 public static void Can_set_and_get_MatchTimeout()
 {
     var attribute = new RegularExpressionAttribute("SomePattern");
     attribute.MatchTimeoutInMilliseconds = 12345;
     Assert.Equal(12345, attribute.MatchTimeoutInMilliseconds);
 }
 public static void Can_construct_and_get_pattern()
 {
     var attribute = new RegularExpressionAttribute("SomePattern");
     Assert.Equal("SomePattern", attribute.Pattern);
 }
Exemplo n.º 35
0
 private static IRegExFacet Create(RegularExpressionAttribute attribute, IFacetHolder holder) {
     return new RegExFacetAnnotation(attribute.Pattern, string.Empty, true, attribute.ErrorMessage, holder);
 }
        public void Setup()
        {
            var attribute1 = new StringLengthAttribute(5);
            attribute1.ErrorMessage = "length";
            var attribute2 = new RegularExpressionAttribute("a*");
            attribute2.ErrorMessage = "regex";

            this.validator = new ValidationAttributeValidator(attribute1, attribute2);
        }
 public static string GetRegExErrorMessage(string pattern, string field)
 {
     var attr = new RegularExpressionAttribute(pattern);
     return attr.FormatErrorMessage(field);
 }