public void IsValid()
        {
            var v = new PatternAttribute("^4[0-9]{12}(?:[0-9]{3})?$", RegexOptions.Singleline, "mess");

            Assert.IsTrue(v.IsValid("4408041234567893", null));
            Assert.IsTrue(v.IsValid(4408041234567893UL, null));
            Assert.IsFalse(v.IsValid("", null));
            Assert.IsTrue(v.IsValid(null, null));

            v = new PatternAttribute(@"(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])", RegexOptions.Singleline,
                                     null);
            Assert.IsTrue(v.IsValid("1999-01-01", null));
            Assert.IsFalse(v.IsValid("1999/01-01", null));
        }
示例#2
0
        private static Attribute ConvertToPattern(XmlNhvmRuleConverterArgs rule)
        {
            NhvmPattern patternRule = (NhvmPattern)rule.schemaRule;

            log.Info("Converting to Pattern attribute");
            PatternAttribute thisAttribute = new PatternAttribute();

            thisAttribute.Regex = patternRule.regex;
            if (!string.IsNullOrEmpty(patternRule.regexoptions))
            {
                thisAttribute.Flags = ParsePatternFlags(patternRule.regexoptions);
            }
            if (patternRule.message != null)
            {
                thisAttribute.Message = patternRule.message;
            }
            AssignTagsFromString(thisAttribute, patternRule.tags);

            return(thisAttribute);
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AutomatedGenerator"/> class.
        /// </summary>
        /// <param name="generator">The generator to use to generate the output.</param>
        /// <param name="locator">Code generation locator.</param>
        /// <param name="resolver">The resolver to resolve workspace symbols.</param>
        /// <param name="patternType">The pattern type to use.</param>
        public AutomatedGenerator(IGenerator generator, ILocator locator, IDeclarationResolver resolver, Type patternType)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            if (locator == null)
            {
                throw new ArgumentNullException(nameof(locator));
            }

            if (resolver == null)
            {
                throw new ArgumentNullException(nameof(resolver));
            }

            if (patternType == null)
            {
                throw new ArgumentNullException(nameof(patternType));
            }

            this.generator   = generator;
            this.resolver    = resolver;
            this.locator     = locator;
            this.patternType = patternType;

            this.patternAttribute = FindAttribute <PatternAttribute>(this.patternType);

            // Get the pattern as source declaration from the given resolver.
            var factoryPatternItfDecl = resolver
                                        .Find(patternType.FullName)
                                        .Single();

            this.pattern = factoryPatternItfDecl;
        }
        public void KnownRulesConvertAssing()
        {
            NhvMapping       map = XmlMappingLoader.GetXmlMappingFor(typeof(WellKnownRules));
            NhvmClass        cm  = map.@class[0];
            XmlClassMapping  rm  = new XmlClassMapping(cm);
            MemberInfo       mi;
            List <Attribute> attributes;

            mi         = typeof(WellKnownRules).GetField("AP");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            Assert.AreEqual("A string value", ((ACustomAttribute)attributes[0]).Value1);
            Assert.AreEqual(123, ((ACustomAttribute)attributes[0]).Value2);
            Assert.AreEqual("custom message", ((ACustomAttribute)attributes[0]).Message);

            mi         = typeof(WellKnownRules).GetField("StrProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            NotEmptyAttribute nea = FindAttribute <NotEmptyAttribute>(attributes);

            Assert.AreEqual("not-empty message", nea.Message);

            NotNullAttribute nna = FindAttribute <NotNullAttribute>(attributes);

            Assert.AreEqual("not-null message", nna.Message);

            NotNullNotEmptyAttribute nnea = FindAttribute <NotNullNotEmptyAttribute>(attributes);

            Assert.AreEqual("notnullnotempty message", nnea.Message);

            LengthAttribute la = FindAttribute <LengthAttribute>(attributes);

            Assert.AreEqual("length message", la.Message);
            Assert.AreEqual(1, la.Min);
            Assert.AreEqual(10, la.Max);

            PatternAttribute pa = FindAttribute <PatternAttribute>(attributes);

            Assert.AreEqual("pattern message", pa.Message);
            Assert.AreEqual("[0-9]+", pa.Regex);
            Assert.AreEqual(RegexOptions.Compiled, pa.Flags);

            EmailAttribute ea = FindAttribute <EmailAttribute>(attributes);

            Assert.AreEqual("email message", ea.Message);

            IPAddressAttribute ipa = FindAttribute <IPAddressAttribute>(attributes);

            Assert.AreEqual("ipAddress message", ipa.Message);

            EANAttribute enaa = FindAttribute <EANAttribute>(attributes);

            Assert.AreEqual("ean message", enaa.Message);

            CreditCardNumberAttribute ccna = FindAttribute <CreditCardNumberAttribute>(attributes);

            Assert.AreEqual("creditcardnumber message", ccna.Message);

            IBANAttribute iban = FindAttribute <IBANAttribute>(attributes);

            Assert.AreEqual("iban message", iban.Message);

            mi         = typeof(WellKnownRules).GetField("DtProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            FutureAttribute fa = FindAttribute <FutureAttribute>(attributes);

            Assert.AreEqual("future message", fa.Message);
            PastAttribute psa = FindAttribute <PastAttribute>(attributes);

            Assert.AreEqual("past message", psa.Message);

            mi         = typeof(WellKnownRules).GetField("DecProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            DigitsAttribute dga = FindAttribute <DigitsAttribute>(attributes);

            Assert.AreEqual("digits message", dga.Message);
            Assert.AreEqual(5, dga.IntegerDigits);
            Assert.AreEqual(2, dga.FractionalDigits);

            MinAttribute mina = FindAttribute <MinAttribute>(attributes);

            Assert.AreEqual("min message", mina.Message);
            Assert.AreEqual(100, mina.Value);

            MaxAttribute maxa = FindAttribute <MaxAttribute>(attributes);

            Assert.AreEqual("max message", maxa.Message);
            Assert.AreEqual(200, maxa.Value);

            DecimalMaxAttribute decimalmaxa = FindAttribute <DecimalMaxAttribute>(attributes);

            Assert.AreEqual("decimal max message", decimalmaxa.Message);
            Assert.AreEqual(200.1m, decimalmaxa.Value);

            DecimalMinAttribute decimalmina = FindAttribute <DecimalMinAttribute>(attributes);

            Assert.AreEqual("decimal min message", decimalmina.Message);
            Assert.AreEqual(99.9m, decimalmina.Value);

            mi         = typeof(WellKnownRules).GetField("BProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            AssertTrueAttribute ata = FindAttribute <AssertTrueAttribute>(attributes);

            Assert.AreEqual("asserttrue message", ata.Message);
            AssertFalseAttribute afa = FindAttribute <AssertFalseAttribute>(attributes);

            Assert.AreEqual("assertfalse message", afa.Message);


            mi         = typeof(WellKnownRules).GetField("ArrProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            SizeAttribute sa = FindAttribute <SizeAttribute>(attributes);

            Assert.AreEqual("size message", sa.Message);
            Assert.AreEqual(2, sa.Min);
            Assert.AreEqual(9, sa.Max);

            mi         = typeof(WellKnownRules).GetField("Pattern");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            PatternAttribute spa = FindAttribute <PatternAttribute>(attributes);

            Assert.AreEqual("{validator.pattern}", spa.Message);
            Assert.AreEqual(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", spa.Regex);
            Assert.AreEqual(RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, spa.Flags);
        }
示例#5
0
        /// <summary>
        /// Runs validation on DTO
        /// </summary>
        /// <param name="dto">DTO</param>
        /// <returns>A pair of the success flag, and summary</returns>
        public static KeyValuePair <bool, string> Validate(object dto)
        {
            KeyValuePair <bool, string> result = new KeyValuePair <bool, string>(true, PASSED);

            foreach (var property in dto.GetType().GetProperties())
            {
                //check for required attribute
                RequiredAttribute required = property.GetCustomAttribute <RequiredAttribute>(true);
                if (required != null)
                {
                    //validate required
                    result = ValidateRequired(dto, property);
                    if (!result.Key)
                    {
                        break;
                    }
                }

                if (property.GetValue(dto) == null)
                {
                    //passed required test and is null
                    //meaning object was not required
                    //and wasn't set
                    return(result);
                }

                NumberAttribute number = property.GetCustomAttribute <NumberAttribute>(true);
                if (number != null)
                {
                    result = ValidateIsNumber(dto, property, number);
                    if (!result.Key)
                    {
                        break;
                    }
                }
                LengthAttribute length = property.GetCustomAttribute <LengthAttribute>(true);
                if (length != null)
                {
                    //validate length
                    result = ValidateLength(dto, property, length);
                    if (!result.Key)
                    {
                        break;
                    }
                }


                RangeAttribute range = property.GetCustomAttribute <RangeAttribute>(true);
                if (range != null)
                {
                    //validate range
                    result = ValidateRange(dto, property, range);
                    if (!result.Key)
                    {
                        break;
                    }
                }

                PatternAttribute pattern = property.GetCustomAttribute <PatternAttribute>(true);
                if (pattern != null)
                {
                    //validate pattern
                    result = ValidatePattern(dto, property, pattern);
                    if (!result.Key)
                    {
                        break;
                    }
                }
            }
            return(result);
        }
示例#6
0
        private static KeyValuePair <bool, string> ValidatePattern(object dto, PropertyInfo field, PatternAttribute attrib)
        {
            bool   passed = true;
            string reason = PASSED;
            string value  = field.GetValue(dto).ToString();

            if (!Regex.IsMatch(value, attrib.Pattern))
            {
                passed = false;
                reason = $"Field {field.Name} does not match the specified regex pattern {attrib.Pattern}";
            }
            return(new KeyValuePair <bool, string>(passed, reason));
        }
        public void AttributeCanBeMultiplied()
        {
            PatternAttribute patternAttribute = new PatternAttribute();

            Assert.AreEqual(true, (AttributeUtils.AttributeAllowsMultiple(patternAttribute)));
        }