Esempio n. 1
0
        public void Transform_collection_index_builder_and_condition()
        {
            var validator = new InlineValidator <Person>();

            validator.TransformForEach(x => x.Orders, to: order => order.Amount)
            .Where(amt => amt < 20)
            .OverrideIndexer((person, collection, amt, numericIndex) => $"[{numericIndex}_{amt}]")
            .LessThan(10);

            var result = validator.Validate(new Person {
                Orders = new List <Order> {
                    new Order {
                        Amount = 21
                    },                                           // Fails condition, skips validation
                    new Order {
                        Amount = 12
                    },                                           // Passes condition, fails validation
                    new Order {
                        Amount = 9
                    },                                          // Passes condition, passes validation
                }
            });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Orders[1_12]");
        }
Esempio n. 2
0
        public void Transforms_collection_element()
        {
            var validator = new InlineValidator <Person>();

            validator.TransformForEach(x => x.Orders, order => order.Amount)
            .GreaterThan(0);

            var result = validator.Validate(new Person()
            {
                Orders = new List <Order> {
                    new Order()
                }
            });

            result.Errors.Count.ShouldEqual(1);
        }
Esempio n. 3
0
        public async Task Transforms_collection_element_async()
        {
            var validator = new InlineValidator <Person>();

            validator.TransformForEach(x => x.Orders, order => order.Amount)
            .MustAsync((amt, token) => Task.FromResult(amt > 0));

            var result = await validator.ValidateAsync(new Person()
            {
                Orders = new List <Order> {
                    new Order()
                }
            });

            result.Errors.Count.ShouldEqual(1);
        }
Esempio n. 4
0
        public async Task Transforms_collection_element_with_propagated_original_object_async()
        {
            var validator = new InlineValidator <Person>();

            validator.TransformForEach(x => x.Children, (parent, children) => new { ParentName = parent.Surname, Children = children })
            .Must(context => context.ParentName == context.Children.Surname);

            var child = new Person {
                Surname = "Pupa"
            };
            var result = await validator.ValidateAsync(new Person()
            {
                Surname = "Lupa", Children = new List <Person> {
                    child
                }
            });

            result.IsValid.ShouldBeFalse();
            result.Errors.Count.ShouldEqual(1);
        }