Exemple #1
0
        public override Exception Validate(object value, bool throwOnInvalid)
        {
            var result = base.Validate(value, throwOnInvalid);

            if (result != null)
            {
                return(result);
            }

            if (value is IValueBM)
            {
                result = ValidateValueInternal(((IValueBM)value).Value, ((IValueBM)value).Settings, (IValueBM)value, throwOnInvalid);
                if (result != null)
                {
                    return(result);
                }
            }
            else
            {
                throw new ArgumentException("Invalid type!", nameof(value));
            }

            var ex = new ValueValidationException(value, new NotImplementedException("{9E172533-449F-46A9-BCE7-83B0ABDA409B}"));

            if (throwOnInvalid)
            {
                throw ex;
            }
            return(ex);
        }
Exemple #2
0
        public override Exception Validate(object value, object rules, bool throwOnInvalid)
        {
            var result = base.Validate(value, rules, throwOnInvalid);

            if (result != null)
            {
                return(result);
            }

            if (value is IValueBM)
            {
                result = ValidateValueInternal(((IValueBM)value).Value, (IValueSettings)rules, (IObjectBM)value, throwOnInvalid);
                if (result != null)
                {
                    return(result);
                }
            }
            else
            {
                result = ValidateValueInternal(value, (IValueSettings)rules, null, throwOnInvalid);
                if (result != null)
                {
                    return(result);
                }
            }

            var ex = new ValueValidationException(value, new NotImplementedException("{81B8738C-B3E9-4A33-AD9D-955FB67DE310}"));

            if (throwOnInvalid)
            {
                throw ex;
            }
            return(ex);
        }
Exemple #3
0
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]        //REVISE: too complex
        protected virtual Exception ValidateValueInternal(object value, IValueSettings rules, IObjectBM businessObject, bool throwOnInvalid)
        {
            //		    var result=base.Validate(value, rules, throwOnError);
            //		    if(!throwOnError && !result) return false;

            #region Null Validator

            if (Equals(value, null))
            {
                if (!rules.AllowNull)
                {
                    var ex = new ValueValidationException(value, businessObject, new ArgumentNullException(nameof(value), "Value must be not null!"));
                    if (!throwOnInvalid)
                    {
                        return(ex);
                    }
                    throw ex;
                }
                return(null);            // if null allowed all other test are skipped
            }

            #endregion

            // at this point value is never null

            #region Empty Validator

            if (value is string && Equals(value, string.Empty))
            {
                if (!rules.AllowEmpty)
                {
                    var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be an empty string!"));
                    if (!throwOnInvalid)
                    {
                        return(ex);
                    }
                    throw ex;
                }
                return(null);            // if empty string allowed all other test are skipped
            }

            #endregion

            // at this point value is never null nor empty string

            if (value is IComparable)
            {
                // this is true for all simple types (Int32, Boolean, etc.) and for String, DateTime, TimeSpan, Guid

                if (rules.MinimumSpecified)
                {
                    if (Comparer.Default.Compare(rules.Minimum, value) > 0)
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be less then Minimum!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.MaximumSpecified)
                {
                    if (Comparer.Default.Compare(value, rules.Maximum) > 0)
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be greater then Maximum!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.IncludeValuesSpecified)
                {
                    if (!rules.IncludeValues.Cast <object>().Any(a => Comparer.Default.Compare(a, value) == 0))
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must be in IncludeValues"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.ExcludeValuesSpecified && rules.ExcludeValues != null)
                {
                    if (rules.ExcludeValues.Cast <object>().Any(a => Comparer.Default.Compare(a, value) == 0))
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be in EcludeValues!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Type is not comparable! ValidateValueInternal must be overridden! " + value.GetType().FullName + ", Implementing Type: " + this.GetType().FullName);
            }

            return(null);
        }