Exemple #1
0
        public void Sanitize(Value value)
        {
            value.FieldName = Name ?? string.Empty;
            string sanitizedValue = TrimField ? value.OriginalValue.Trim()
                                              : value.OriginalValue;

            //If it wasn't in the original file, handle it in advance
            if (value.Missing || sanitizedValue == string.Empty)
            {
                if (!AllowedBlank)
                {
                    value.ErrorMsg = ValueIfBlank;
                }
                return;
            }

            //Removes any disallowed characters and retains only allowed characters
            var chars = sanitizedValue.ToCharArray();

            if (AllowedCharacters.Any())
            {
                chars = chars.Where(c => AllowedCharacters.Any(a => a == c)).ToArray();
            }
            if (DisallowedCharacters.Any())
            {
                chars = chars.Where(c => !DisallowedCharacters.Any(d => d == c)).ToArray();
            }

            //validate the length
            if (chars.Length > MaxLength ||
                chars.Length < MinLength)
            {
                value.SanitizedValue = new string(chars);
                value.ErrorMsg       = ValueIfWrongLength;
                return;
            }

            //run regex
            sanitizedValue = new string(chars);
            if (RegEx != string.Empty && !Regex.IsMatch(sanitizedValue, RegEx))
            {
                value.ErrorMsg = "Value failed regex check on value.";
                return;
            }

            //run any custom checks
            value.SanitizedValue = RemoveDoubleSpaces(sanitizedValue); //remove any double spaces
            CustomChecks.ForEach(c => c.Execute(value));

            //If this there are a fixed number of options, check for them
            if ((AllowedValues?.Count() ?? 0) != 0 &&
                !AllowedValues.Any(v => v == value.SanitizedValue))
            {
                value.ErrorMsg = ValueIfNotInAllowedOptions;
            }
        }
Exemple #2
0
        public string MatcherRegEx(bool hasNext)
        {
            // <p (id=#([0-9.*{6})|sdf).*>

            var regExp = new StringBuilder();

            regExp.Append(Name)
            .Append(Consts.ANY_NORMAL_WHITESPACES)
            .Append("=")
            .Append(Consts.ANY_NORMAL_WHITESPACES)
            .Append("\"")
            .Append(Consts.OPEN_ATTRIBUTE);

            bool hasRegExps = AllowedRegExps.Any();

            if (AllowedRegExps.Count() + AllowedValues.Count() > 0)
            {
                foreach (string allowedValue in AllowedValues)
                {
                    regExp.Append(DocumentTag.EscapeRegularExpressionCharacters(allowedValue));

                    if (AllowedValues.Last() != allowedValue || hasRegExps)
                    {
                        regExp.Append(Consts.ATTRIBUTE_DIVIDER);
                    }
                }

                foreach (string allowedRegExp in AllowedRegExps)
                {
                    regExp.Append(allowedRegExp);
                    if (AllowedRegExps.Last() != allowedRegExp)
                    {
                        regExp.Append(Consts.ATTRIBUTE_DIVIDER);
                    }
                }

                if (this.AllowedRegExps.Count() + this.AllowedValues.Count() > 0)
                {
                    regExp.Append(Consts.CLOSE_ATTRIBUTE);
                }

                regExp.Append("\"" + Consts.ANY_NORMAL_WHITESPACES);

                if (hasNext)
                {
                    regExp.Append(Consts.ATTRIBUTE_DIVIDER);
                }
            }
            return(regExp.ToString());
        }