Ejemplo n.º 1
0
 public ValidationResult(bool valid, string itemId, IEnumerable <string> messages, IEnumerable <IValidationResult> nestedResults)
 {
     ItemId        = itemId;
     Valid         = valid;
     Messages      = Immutable.ReadOnlyList(messages);
     NestedResults = Immutable.ReadOnlyList(nestedResults);
 }
Ejemplo n.º 2
0
        public GroupedItems <T, K, I> GroupItems(IEnumerable <T> items)
        {
            List <I>            nullTo   = null;
            List <Pair <I, T> > nonKeyTo = null;
            IDictionary <K, IList <Pair <I, T> > > groupItems = null;

            // Perform grouping
            if (CollectionUtil.IsNotNullOrEmpty(items))
            {
                int i = 0;
                foreach (var item in items)
                {
                    I index = indexCreator(item, i++);
                    // register any nulls
                    if (item == null)
                    {
                        if (nullTo == null)
                        {
                            nullTo = new List <I>();
                        }
                        nullTo.Add(index);
                    }
                    else
                    {
                        K key = keyCreator(item);
                        if (key == null)
                        {
                            // register null key
                            if (nonKeyTo == null)
                            {
                                nonKeyTo = new List <Pair <I, T> >();
                            }
                            nonKeyTo.Add(new Pair <I, T>(index, item));
                        }
                        else
                        {
                            // register keyed item
                            if (groupItems == null)
                            {
                                groupItems = new Dictionary <K, IList <Pair <I, T> > >();
                            }
                            CollectionUtil.AddToMappedList(groupItems, key, new Pair <I, T>(index, item));
                        }
                    }
                }
            }
            return(new GroupedItems <T, K, I>()
            {
                NullItems = nullTo ?? Immutable.ReadOnlyList <I>(),
                NullKeyItems = nonKeyTo ?? Immutable.ReadOnlyList <Pair <I, T> >(),
                GroupItems = groupItems ?? Immutable.Dictionary <K, IList <Pair <I, T> > >()
            });
        }
Ejemplo n.º 3
0
        public override string ToString()
        {
            var b = new StringBuilder("Validation Item (")
                    .Append(ItemId)
                    .Append(") ")
                    .Append(Valid ? "[Valid]" : "[Invalid]")
                    .Append(" {")
                    .Append(string.Join(",", Messages ?? Immutable.ReadOnlyList <string>()))
                    .Append(" }");

            if (CollectionUtil.IsNotNullOrEmpty(NestedResults))
            {
                b.Append(" Nested (").Append(NestedResults.Count).Append(") [")
                .Append(Environment.NewLine)
                .Append(string.Join(Environment.NewLine, NestedResults ?? Immutable.ReadOnlyList <IValidationResult>()))
                .Append(Environment.NewLine)
                .Append("]");
            }
            return(b.ToString());
        }
Ejemplo n.º 4
0
 public ValidationResult(bool valid, string itemId, string message)
 {
     ItemId   = itemId;
     Valid    = valid;
     Messages = Immutable.ReadOnlyList(message);
 }
Ejemplo n.º 5
0
 public static IValidationResult MergeFailedResultsAsNested(string itemId, IEnumerable <IValidationResult> results)
 {
     if (CollectionUtil.IsNotNullOrEmpty(results))
     {
         bool allPassed = true;
         List <IValidationResult> nested = new List <IValidationResult>();
         foreach (var result in results)
         {
             if (!result.Valid)
             {
                 allPassed = false;
                 nested.Add(result);
             }
         }
         return(allPassed ? (IValidationResult)PassValidationResult.Instance : new ValidationResult(false, itemId, Immutable.ReadOnlyList <string>(), nested));
     }
     return(PassValidationResult.Instance);
 }
 public IReadOnlyList <string> Adapt(string input)
 {
     return(input == null?Immutable.ReadOnlyList <string>() : Immutable.ReadOnlyList(input.Split(separator)));
 }
 public CombinationValidator(IEnumerable <IValidator <T> > validators, bool mergeAsNested)
 {
     this.validators    = Immutable.ReadOnlyList(validators);
     this.mergeAsNested = mergeAsNested;
 }