Exemple #1
0
        public RegularExpressionValidator(string method, string pattern, string explanation, IContext context = null) : base(context)
        {
            _method = method;

            if (IsMissingContext())
            {
                return;
            }
#if NETS10
            _regex = new Regex(pattern);
#else
            _regex = new Regex(pattern, RegexOptions.Compiled);
#endif

            if (!Run)
            {
                return;
            }
            _input = SingleInput();
            var help = Context.Field.Help;
            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} {explanation}.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #2
0
        public InValidator(IContext context) : base(context)
        {
            if (!Run)
            {
                return;
            }

            if (IsMissing(context.Operation.Domain))
            {
                return;
            }

            _input = SingleInput();
            var items = Utility.Split(Context.Operation.Domain, ',');

            foreach (var item in items)
            {
                try {
                    _set.Add(_input.Convert(item));
                } catch (Exception ex) {
                    context.Warn($"In transform can't convert {item} to {_input.Type} {ex.Message}.");
                }
            }

            _readableDomain = Utility.ReadableDomain(items);
            var help = context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{context.Field.Label} must be in {_readableDomain}.";
            }
            _betterFormat = new BetterFormat(context, help, context.Entity.GetAllFields);
        }
Exemple #3
0
        public EndsWithValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsMissing(Context.Operation.Value))
            {
                return;
            }
            _input = SingleInput();
            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} must end with {Context.Operation.Value}.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #4
0
        public IsValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsMissing(Context.Operation.Type))
            {
                return;
            }
            _input        = SingleInput();
            _isCompatible = _input.Type == Context.Operation.Type || _input.IsNumeric() && Context.Operation.Type == "double";
            _canConvert   = v => _converter[Context.Operation.Type](v);

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{_input.Label}'s value is incompatable with the {Context.Operation.Type} data type.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #5
0
        public MatchValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            _input = SingleInput();
#if NETS10
            _regex = new Regex(context.Operation.Pattern);
#else
            _regex = new Regex(Context.Operation.Pattern, RegexOptions.Compiled);
#endif

            var help = Context.Field.Help;
            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} must match the regular expression pattern: {Context.Operation.Pattern.Replace("{", "{{").Replace("}", "}}")}.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #6
0
        public DefaultValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            _input = SingleInput();
            var types = Constants.TypeDefaults();

            _default = _input.Default == Constants.DefaultSetting ? types[_input.Type] : _input.Convert(_input.Default);

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} must be the default value of {_default}.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
        public NumericValidator(IContext context) : base(context)
        {
            if (!Run)
            {
                return;
            }

            var input = SingleInput();

            if (input.IsNumeric())
            {
                _transform = row => true;
            }
            else
            {
                _transform = row => {
                    double val;
                    return(double.TryParse(GetString(row, input), out val));
                };
            }
            var help = context.Field.Help;

            if (help == string.Empty)
            {
                help = $"The value {{{context.Field.Alias}}} in {context.Field.Label} must be numeric.";
            }
            _betterFormat = new BetterFormat(context, help, context.Entity.GetAllFields);
        }
Exemple #8
0
        public NumericValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            var input = SingleInput();

            if (input.IsNumericType())
            {
                _validator = row => true;
            }
            else
            {
                _validator = row => double.TryParse(GetString(row, input), out _);
            }
            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"The value {{{Context.Field.Alias}}} in {Context.Field.Label} must be numeric.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #9
0
        public NotEqualValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            Field[] rest;
            bool    sameTypes;
            var     input = MultipleInput();
            var     first = input.First();

            if (Context.Operation.Value == Constants.DefaultSetting)
            {
                rest      = input.Skip(1).ToArray();
                sameTypes = rest.All(f => f.Type == first.Type);
            }
            else
            {
                _value    = first.Convert(Context.Operation.Value);
                rest      = input.ToArray();
                sameTypes = input.All(f => f.Type == first.Type);
            }

            if (sameTypes)
            {
                if (_value == null)
                {
                    _validator = row => !rest.All(f => row[f].Equals(row[first]));
                }
                else
                {
                    _validator = row => !rest.All(f => row[f].Equals(_value));
                }
            }
            else
            {
                _validator = row => true;
            }

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                if (_value == null)
                {
                    help = $"{Context.Field.Label} must not equal {{{first.Alias}}} in {first.Label}.";
                }
                else
                {
                    help = $"{Context.Field.Label} must not equal {_value}.";
                }
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #10
0
        public EmptyValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsNotReceiving("string"))
            {
                return;
            }
            _input = SingleInput();
            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} must be empty.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #11
0
        public ContainsValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsMissing(Context.Operation.Value))
            {
                return;
            }
            _input        = MultipleInput();
            _valueIsField = Context.Entity.TryGetField(Context.Operation.Value, out _valueField);

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                if (_valueIsField)
                {
                    help = $"{Context.Field.Label} must contain {{{_valueField.Alias}}}.";
                }
                else
                {
                    help = $"{Context.Field.Label} must contain {Context.Operation.Value}.";
                }
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #12
0
        public CompareValidator(IContext context, string type) : base(context)
        {
            string explanation;

            switch (type)
            {
            case "min":
                explanation = "must be greater than or equal to";
                break;

            case "max":
                explanation = "must be less than or equal to";
                break;

            default:
                Run = false;
                return;
            }

            var input = SingleInput();
            var help  = context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{context.Field.Label} {explanation} {context.Operation.Value}.";
            }

            var isComparable = Constants.TypeDefaults()[input.Type] is IComparable;

            if (!isComparable)
            {
                context.Error($"you can't use {type} validator on {input.Alias} field.  It's type is not comparable.");
                Run = false;
                return;
            }

            var isField = context.Entity.FieldMatcher.IsMatch(context.Operation.Value);

            if (isField)
            {
                var field = context.Entity.GetField(context.Operation.Value);
                _validator = delegate(IRow row) {
                    var inputValue = (IComparable)row[input];
                    var otherValue = row[field];
                    return(type == "min" ? inputValue.CompareTo(otherValue) > -1 : inputValue.CompareTo(otherValue) < 1);
                };
            }
            else
            {
                var value = input.Convert(context.Operation.Value);
                _validator = delegate(IRow row) {
                    var inputValue = (IComparable)row[input];
                    var otherValue = value;
                    return(type == "min" ? inputValue.CompareTo(otherValue) > -1 : inputValue.CompareTo(otherValue) < 1);
                };
            }

            _betterFormat = new BetterFormat(context, help, context.Entity.GetAllFields);
        }
Exemple #13
0
        public LengthValidator(IContext context) : base(context)
        {
            if (!Run)
            {
                return;
            }
            _input = SingleInput();
            var help = context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{context.Field.Label} must be {context.Operation.Length} characters.";
            }
            _betterFormat = new BetterFormat(context, help, context.Entity.GetAllFields);
        }
        public RequiredValidator(IContext context) : base(context)
        {
            if (!Run)
            {
                return;
            }
            var defaults = Constants.TypeDefaults();

            _input   = SingleInput();
            _default = _input.Default == Constants.DefaultSetting ? defaults[_input.Type] : _input.Convert(_input.Default);

            var help = context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{context.Field.Label} is required.";
            }
            _betterFormat = new BetterFormat(context, help, context.Entity.GetAllFields);
        }
Exemple #15
0
        public override IEnumerable <IRow> Operate(IEnumerable <IRow> rows)
        {
            var map = CreateMap();

            foreach (var item in map.Items)
            {
                _map.Add(_input.Convert(item.From));
            }

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label}'s value {{{Context.Field.Alias}}} is not found in {_map.Count} items.";
            }
            _betterFormat = new BetterFormat(Context, help, Context.Entity.GetAllFields);

            return(base.Operate(rows));
        }
Exemple #16
0
        public MaxLengthValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }
            _input = SingleInput();
            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} must have {Context.Operation.Length} or less characters.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
        public AllValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsMissing(Context.Operation.Operator))
            {
                return;
            }

            if (IsMissing(Context.Operation.Value))
            {
                return;
            }

            foreach (var field in MultipleInput())
            {
                if (Constants.CanConvert()[field.Type](Context.Operation.Value))
                {
                    _input.Add(new FieldWithValue {
                        Field = field, Value = field.Convert(Context.Operation.Value)
                    });
                }
            }

            _func = GetFunc(Context.Operation.Operator);
            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"All of the field(s): {Utility.ReadableDomain(_input.Select(f => f.Field.Alias))} must be {Context.Operation.Operator.TrimEnd('s')} to {Context.Operation.Value}.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #18
0
        public RequiredValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }
            var defaults = Constants.TypeDefaults();

            _input   = SingleInput();
            _default = defaults[_input.Type];

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                help = $"{Context.Field.Label} is required.";
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);
        }
Exemple #19
0
        public override IEnumerable <IRow> Operate(IEnumerable <IRow> rows)
        {
            /* Over ride Operate(IEnumerable<IRow>) to load the map, which may not be available at start up */

            foreach (var item in CreateMap().Items)
            {
                _map.Add(_input.Convert(item.From));
            }

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                if (_autoMap || _map.Count > 5)
                {
                    help = $"{Context.Field.Label}'s value {{{Context.Field.Alias}}} is {(_inMap ? "not one of" : "one of")} {_map.Count} {(_inMap ? "valid" : "invalid")} items.";
                }
                else
                {
                    var domain = Utility.ReadableDomain(_map);
                    if (domain == string.Empty)
                    {
                        help = $"{Context.Field.Label} has an empty {Context.Operation.Method} validator.";
                    }
                    else
                    {
                        help = $"{Context.Field.Label}'s value {{{Context.Field.Alias}}} must {(_inMap ? "be" : "not be")} one of these {_map.Count} items: " + domain + ".";
                    }
                }
            }

            _betterFormat = new BetterFormat(Context, help, Context.Entity.GetAllFields);

            Func <object, bool> isValid;

            if (_inMap)
            {
                isValid = o => _map.Contains(o);
            }
            else
            {
                isValid = o => !_map.Contains(o);
            }

            /* when you override Operate(IEnumerable<IRow>) you must add your ShouldRun check, if you want it */
            if (Context.Operation.ShouldRun == null)
            {
                _validate = row => {
                    if (IsInvalid(row, isValid(row[_input])))
                    {
                        AppendMessage(row, _betterFormat.Format(row));
                    }
                };
            }
            else
            {
                _validate = row => {
                    if (Context.Operation.ShouldRun(row) && IsInvalid(row, isValid(row[_input])))
                    {
                        AppendMessage(row, _betterFormat.Format(row));
                    }
                };
            }

            return(rows.Select(Operate));
        }
Exemple #20
0
        public override IEnumerable <IRow> Operate(IEnumerable <IRow> rows)
        {
            /* Over ride Operate(IEnumerable<IRow>) to load the map, which may not be available at start up */

            var mapItems = CreateMap().Items;
            var usingTo  = mapItems.Any(mi => !mi.To.Equals(Constants.DefaultSetting));

            foreach (var item in CreateMap().Items)
            {
                var from = _input.Convert(item.From);
                if (usingTo)
                {
                    var to = _input.Convert(item.To);
                    _map[to.Equals(Constants.DefaultSetting) ? from : to] = from;
                }
                else
                {
                    _map[from] = from;
                }
            }

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                if (_autoMap || _map.Count > 5)
                {
                    if (usingTo)
                    {
                        help = $"{Context.Field.Label} is {(_inMap ? "not one of" : "one of")} {_map.Count} {(_inMap ? "valid" : "invalid")} items.";
                    }
                    else
                    {
                        help = $"{Context.Field.Label}'s value {{{Context.Field.Alias}}} is {(_inMap ? "not one of" : "one of")} {_map.Count} {(_inMap ? "valid" : "invalid")} items.";
                    }
                }
                else
                {
                    var domain = Utility.ReadableDomain(_map.Values);
                    if (domain == string.Empty)
                    {
                        help = $"{Context.Field.Label} has an empty {Context.Operation.Method} validator.";
                    }
                    else
                    {
                        if (usingTo)
                        {
                            help = $"{Context.Field.Label} must {(_inMap ? "be" : "not be")} one of these {_map.Count} items: " + domain + ".";
                        }
                        else
                        {
                            help = $"{Context.Field.Label}'s value {{{Context.Field.Alias}}} must {(_inMap ? "be" : "not be")} one of these {_map.Count} items: " + domain + ".";
                        }
                    }
                }
            }

            _betterFormat = new BetterFormat(Context, help, Context.Entity.GetAllFields);

            Func <object, bool> isValid;

            if (_inMap)
            {
                // isValid = o => _map.Contains(o);
                isValid = o => _map.ContainsKey(o);
            }
            else
            {
                // isValid = o => !_map.Contains(o);
                isValid = o => !_map.ContainsKey(o);
            }

            _validate = row => {
                if (IsInvalid(row, isValid(row[_input])))
                {
                    AppendMessage(row, _betterFormat.Format(row));
                }
            };

            return(rows.Select(Operate));
        }
Exemple #21
0
        public ContainsValidator(IContext context = null) : base(context)
        {
            if (IsMissingContext())
            {
                return;
            }

            if (!Run)
            {
                return;
            }

            if (IsMissing(Context.Operation.Value))
            {
                return;
            }

            _input        = MultipleInput();
            _field        = _input[0];
            _valueIsField = Context.Entity.TryGetField(Context.Operation.Value, out _valueField);

            var nextOperation = NextOperation();
            var inverted      = nextOperation != null && nextOperation.Method == "invert";

            var help = Context.Field.Help;

            if (help == string.Empty)
            {
                if (inverted)
                {
                    if (_valueIsField)
                    {
                        help = $"{Context.Field.Label} must not contain {{{_valueField.Alias}}}.";
                    }
                    else
                    {
                        help = $"{Context.Field.Label} must not contain {Context.Operation.Value}.";
                    }
                }
                else
                {
                    if (_valueIsField)
                    {
                        help = $"{Context.Field.Label} must contain {{{_valueField.Alias}}}.";
                    }
                    else
                    {
                        help = $"{Context.Field.Label} must contain {Context.Operation.Value}.";
                    }
                }
            }
            _betterFormat = new BetterFormat(context, help, Context.Entity.GetAllFields);

            if (_input.Length > 1)
            {
                if (inverted)
                {
                    _isValid = (row, text) => {
                        return(!_input.Any(f => GetString(row, f).Contains(text)));
                    };
                }
                else
                {
                    _isValid = (row, text) => {
                        return(_input.Any(f => GetString(row, f).Contains(text)));
                    };
                }
            }
            else
            {
                if (inverted)
                {
                    _isValid = (row, text) => !GetString(row, _field).Contains(text);
                }
                else
                {
                    _isValid = (row, text) => GetString(row, _field).Contains(text);
                }
            }
        }