Exemplo n.º 1
0
        public static TSource FixValue <TSource>(this IValidationRepository repository, TSource value, string valueName, Action <PathValidation <object, TSource> > init)
        {
            var factory = repository.Create((object)value);
            var path    = new PathValidation <object, TSource>(null, factory);

            init(path);

            object objValue = value;

            var context = new ValidationContext(valueName, value);
            var result  = new ValidationResult();

            foreach (var tValidator in path.Validators)
            {
                result.Load(tValidator.Validate(context, ref objValue));
            }

            result.ThrowIssues();

            context.WithUpdate = true;

            foreach (var tValidator in path.Validators)
            {
                tValidator.Validate(context, ref objValue);
            }

            return((TSource)objValue);
        }
Exemplo n.º 2
0
 public static PathValidation <TSource, string> LengthBetween <TSource>(this PathValidation <TSource, string> validation, int?minLength, int?maxLength)
 {
     validation.AddValidator(new LengthBetweenRule()
     {
         MinLength = minLength, MaxLength = maxLength
     });
     return(validation);
 }
Exemplo n.º 3
0
 public static PathValidation <TSource, TResult> FixLength <TSource, TResult>(this PathValidation <TSource, TResult> validation, int length)
 {
     validation.AddValidator(new MaxLength()
     {
         Length = length
     });
     return(validation);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Validate that value greate then 1900-01-01
 /// </summary>
 public static PathValidation <TSource, DateTime> NewEra <TSource>(this PathValidation <TSource, DateTime> validation)
 {
     validation.AddValidator(new CompareRule.NotLesser()
     {
         Value = new DateTime(1900, 1, 1)
     });
     return(validation);
 }
Exemplo n.º 5
0
 public static PathValidation <TSource, TResult> Length <TSource, TResult>(this PathValidation <TSource, TResult> validation, int length) where TResult : IEnumerable
 {
     validation.AddValidator(new LengthBetweenRule()
     {
         MinLength = length, MaxLength = length
     });
     return(validation);
 }
Exemplo n.º 6
0
 public static PathValidation <TSource, TResult> Forbidden <TSource, TResult>(this PathValidation <TSource, TResult> validation, params TResult[] items)
 {
     validation.AddValidator(new ForbiddenRule()
     {
         Items = items.Cast <object>().ToList()
     });
     return(validation);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Set specific value if the field has default(TResult)
 /// </summary>
 public static PathValidation <TSource, TResult> FixEmpty <TSource, TResult>(this PathValidation <TSource, TResult> validation, TResult defaultValue)
 {
     validation.AddValidator(new DefaultRule(typeof(TResult))
     {
         Default = () => defaultValue
     });
     return(validation);
 }
Exemplo n.º 8
0
 public static PathValidation <TSource, TResult> Available <TSource, TResult>(this PathValidation <TSource, TResult> validation, object[] values)
 {
     validation.AddValidator(new AvailableRule()
     {
         Values = values.ToList()
     });
     return(validation);
 }
Exemplo n.º 9
0
 public static PathValidation <TSource, string> FixLatinFileName <TSource>(this PathValidation <TSource, string> validation)
 {
     validation.AddValidator(new FileNameRule()
     {
         LatinOnly = true
     });
     return(validation);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Set DateTime.UtcNow if a value earlear that UtcNow
 /// </summary>
 public static PathValidation <TSource, DateTime> FixEarlierNow <TSource>(this PathValidation <TSource, DateTime> validation)
 {
     validation.AddValidator(new CompareNowRule.Later()
     {
         FixToNow = true
     });
     return(validation);
 }
Exemplo n.º 11
0
        public static PathValidation <TSource, TResult> Fix <TSource, TResult>(this PathValidation <TSource, TResult> validation, Func <TResult, TResult> fixFunc)
        {
            validation.AddValidator(new FixRule <TResult>()
            {
                FixFunc = fixFunc
            });

            return(validation);
        }
Exemplo n.º 12
0
        public static PathValidation <TSource, TResult> Complex <TSource, TResult>(this PathValidation <TSource, TResult> validation,
                                                                                   ValidationRuleFactory <TResult> rules)
        {
            validation.AddValidator(new ComplexRule <TResult>()
            {
                Factory = rules
            });

            return(validation);
        }
Exemplo n.º 13
0
        public static PathValidation <TSource, TResult> Check <TSource, TResult>(this PathValidation <TSource, TResult> validation,
                                                                                 Func <ValidationContext, TResult, IEnumerable <ValidationIssue> > ruleFunc)
        {
            validation.AddValidator(new CheckRule <TResult>()
            {
                ValidateFunc = ruleFunc
            });

            return(validation);
        }
Exemplo n.º 14
0
        public PathValidation <TSource, TResult> AddToValidation <TSource>(PathValidation <TSource, TProperty> validation)
        {
            var tValidator = new PathValidation <TSource, TResult>(validation.Indexer, validation.Factory);

            tValidator.Id   = validation.Id;
            tValidator.Path = validation.Path;

            tValidator.Validators = validation.Validators;
            tValidator.AddValidator(this);

            return(tValidator);
        }
Exemplo n.º 15
0
        ValidationResult ValidateObject(PathValidation <TSource> path, ValidationContext context, object[] valueStor)
        {
            var result       = new ValidationResult();
            var currentValue = valueStor[0];

            foreach (var tValidator in path.Validators)
            {
                result.Load(tValidator.Validate(context, ref currentValue));
            }

            valueStor[0] = currentValue;
            return(result);
        }
Exemplo n.º 16
0
        public static PathValidation <TSource, TResult> Complex <TSource, TResult>(this PathValidation <TSource, TResult> validation, Action <ValidationRuleFactory <TResult> > prepareRules)
        {
            var factory = validation.Factory.CreateBySettings <TResult>();

            prepareRules(factory);

            validation.AddValidator(new ComplexRule <TResult>()
            {
                Factory = factory
            });

            return(validation);
        }
Exemplo n.º 17
0
        public PathValidation <TSource, TProperty> RuleForIf <TProperty>(IPath <TSource> path, Func <TSource, bool> condition)
        {
            var indexer    = Repository.GetIndexer(path);
            var tValidator = new PathValidation <TSource, TProperty>(indexer, this);

            m_validators.Add(tValidator);

            if (condition != null)
            {
                m_conditions[tValidator.Id] = condition;
            }

            return(tValidator);
        }
Exemplo n.º 18
0
        public void Validate <TSource>(PathValidation <TSource> validation, Type sourceType)
        {
            if (sourceType == null)
            {
                return;
            }

            var methods = m_invoker.GetValidators(sourceType);

            foreach (var t in methods)
            {
                var tFactory = Activator.CreateInstance(t.FactoryType, validation.Factory.Repository, validation.Factory.OriginalValue);
                t.Invoke(tFactory, this);

                var tRule = Activator.CreateInstance(t.RuleType) as BaseValidationRule;
                ((IComplexAdapterRule)tRule).Init(tFactory, this);

                validation.AddValidator(tRule);
            }
        }
Exemplo n.º 19
0
 public static PathValidation <TSource, string> Base64 <TSource>(this PathValidation <TSource, string> validation)
 {
     validation.AddValidator(new Base64Rule());
     return(validation);
 }
Exemplo n.º 20
0
        public static PathValidation <TSource, TResult> ValidateDto <TSource, TResult>(this PathValidation <TSource, TResult> validation)
        {
            var factory = validation.Factory;

            var dtoComplex = factory.Repository.Extension(typeof(DtoComplex)) as DtoComplex;

            if (dtoComplex == null)
            {
                throw new NotImplementedException($"The '{nameof(ValidateDto)}' method is available for {nameof(DtoComplex)} repository only.");
            }

            validation.AddValidator(new DtoComplexRule <TSource>()
            {
                Complex = dtoComplex,
                Factory = factory
            });

            return(validation);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Don't forget to restore TProperty typr before validation finish.
        /// </summary>
        public static PathValidation <TSource, TResult> Cast <TSource, TProperty, TResult>(this PathValidation <TSource, TProperty> validation, Func <TProperty, TResult> cast)
        {
            var resultValidation = new CastRule <TProperty, TResult>()
            {
                CastFunc = cast
            }
            .AddToValidation(validation);

            return(resultValidation);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Set specific value if the field has default(TResult)
        /// </summary>
        public static PathValidation <TSource, TResult> FixEmpty <TSource, TResult>(this PathValidation <TSource, TResult> validation)
        {
            var defaultValue = (TResult)NotEmptyRule.DefaultValue(typeof(TResult));

            return(FixEmpty(validation, defaultValue));
        }
Exemplo n.º 23
0
 /// <summary>
 /// Enumeration value will check on 0 size. In the case the value will transform to NULL.
 /// </summary>
 public static PathValidation <TSource, TProperty> Trim <TSource, TProperty>(this PathValidation <TSource, TProperty> validation) where TProperty : IEnumerable
 {
     validation.AddValidator(new TrimRule());
     return(validation);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Removed all Empty values from collection field
 /// </summary>
 public static PathValidation <TSource, TResult> SkipEmpty <TSource, TResult>(this PathValidation <TSource, TResult> validation) where TResult : IEnumerable
 {
     validation.AddValidator(new SkipEmptyRule());
     return(validation);
 }
Exemplo n.º 25
0
 public static PathValidation <TSource, TEnume> Enum <TSource, TEnume>(this PathValidation <TSource, TEnume> validation)
 {
     validation.AddValidator(new EnumRule());
     return(validation);
 }
Exemplo n.º 26
0
 public static PathValidation <TSource, TResult> RoundMantissa <TSource, TResult>(this PathValidation <TSource, TResult> validation, int length)
 {
     validation.AddValidator(new MantissaLength()
     {
         Length = length
     });
     return(validation);
 }
Exemplo n.º 27
0
 public static PathValidation <TSource, TProperty> Greater <TSource, TProperty>(this PathValidation <TSource, TProperty> validation, TProperty minValue)
 {
     validation.AddValidator(new CompareRule.Greater()
     {
         Value = minValue
     });
     return(validation);
 }
Exemplo n.º 28
0
 /// <summary>
 /// Correct value to max value if a value greater then the max value
 /// </summary>
 public static PathValidation <TSource, TProperty> FixGreater <TSource, TProperty>(this PathValidation <TSource, TProperty> validation, TProperty maxValue)
 {
     validation.AddValidator(new CompareRule.NotGreater()
     {
         Value = maxValue, FixToValue = true
     });
     return(validation);
 }
Exemplo n.º 29
0
 public static PathValidation <TSource, string> FixToUpper <TSource>(this PathValidation <TSource, string> validation)
 {
     validation.AddValidator(new ToUpperRule());
     return(validation);
 }
Exemplo n.º 30
0
 public static PathValidation <TSource, string> Url <TSource>(this PathValidation <TSource, string> validation)
 {
     validation.AddValidator(new UrlRule());
     return(validation);
 }