Esempio n. 1
0
        public Result <TValue, IValidationError> Validate <TValue>(int columnIndex, int rowIndex, string columnName, string propertyName, TValue value)
        {
            try
            {
                var stringValue = value.IsNull() ? string.Empty : value.ToString();

                if (Regex.IsMatch(stringValue, _pattern, _isCaseSensitive ? RegexOptions.IgnoreCase : RegexOptions.None))
                {
                    if (value.IsNull())
                    {
                        if (typeof(TValue) == typeof(string))
                        {
                            var returnValue = (TValue)Convert.ChangeType(string.Empty, typeof(TValue));
                            return(Result.Ok <TValue, IValidationError>(returnValue));
                        }

                        return(Result.Ok <TValue, IValidationError>(default(TValue)));
                    }

                    return(Result.Ok <TValue, IValidationError>(value));
                }

                var validationError = RuleValidationError.ValueDoesNotMatchRegex(columnIndex, rowIndex, columnName,
                                                                                 propertyName, stringValue, _pattern);

                return(Result.Fail <TValue, IValidationError>(validationError));
            }
            catch (Exception)
            {
                var validationError = RuleValidationError.CouldNotValidateValueWithPattern(columnIndex, rowIndex, columnName,
                                                                                           propertyName, _pattern);

                return(Result.Fail <TValue, IValidationError>(validationError));
            }
        }
        public Result <object, IValidationError> Validate(int columnIndex, int rowIndex, string columnName,
                                                          string propertyName, object value)
        {
            try
            {
                var typedValue = (TValue)value;

                if (_customRuleFunc(typedValue))
                {
                    return(Result.Ok <object, IValidationError>(value));
                }

                var validationError = new RuleValidationError(columnIndex, rowIndex, _validationMessage, columnName,
                                                              typedValue.ToString(), propertyName);

                return(Result.Fail <object, IValidationError>(validationError));
            }
            catch (Exception)
            {
                var validationError =
                    RuleValidationError.CouldNotValidateValue(columnIndex, rowIndex, columnName, propertyName);

                return(Result.Fail <object, IValidationError>(validationError));
            }
        }
        public Result <object, IValidationError> Validate(int columnIndex, int rowIndex, string columnName, string propertyName, object value)
        {
            var castValue = (TValue)value;

            if (castValue.CompareTo(_minimumValue) <= 0)
            {
                var validationError = RuleValidationError.HasToHaveMinimumValueOf(columnIndex, rowIndex, columnName, propertyName, _minimumValue);
                return(Result.Fail <object, IValidationError>(validationError));
            }

            return(Result.Ok <object, IValidationError>(_minimumValue));
        }
Esempio n. 4
0
        public Result <List <object>, IValidationError> Validate(int columnIndex, string columnName, List <object> allValuesInColumn)
        {
            if (allValuesInColumn.IsNotNull())
            {
                if (allValuesInColumn.GroupBy(x => x).Any(g => g.Count() > 1))
                {
                    var validationError = RuleValidationError.ColumnMustContainUniqueValues(columnIndex, columnName);
                    return(Result.Fail <List <object>, IValidationError>(validationError));
                }
            }

            return(Result.Ok <List <object>, IValidationError>(allValuesInColumn));
        }