public void CustomAsync_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => validator.CustomAsync(async x => new ValidationFailure("x", "y")));
            validator.RuleSet("bar", () => validator.CustomAsync(async x => new ValidationFailure("x", "y")));

            var result = validator.ValidateAsync(new Person(), ruleSet: "foo").Result;

            result.Errors.Count.ShouldEqual(1);
        }
        public void Custom_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => { validator.Custom(x => { return(new ValidationFailure("x", "y")); }); });
            validator.RuleSet("bar", () => { validator.Custom(x => { return(new ValidationFailure("x", "y")); }); });

            var result = validator.Validate(new Person(), ruleSet: "foo");

            result.Errors.Count.ShouldEqual(1);
        }
예제 #3
0
        public void Trims_spaces()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });
            validator.RuleSet("Second", () => {
                validator.RuleFor(x => x.Surname).NotNull();
            });

            var result = validator.Validate(new Person(), ruleSet: "First, Second");

            result.Errors.Count.ShouldEqual(2);
        }
예제 #4
0
        public void Rulesets_cascade_properly_with_polymorphic_validators()
        {
            var fooValidator = new InlineValidator <FooImpl1>();

            fooValidator.RuleSet("test", () => {
                fooValidator.RuleFor(x => x.Name).NotNull();
            });

            var validator = new InlineValidator <Root>();

            validator.RuleSet("test", () => {
                validator.RuleFor(x => x.Foo).SetInheritanceValidator(v => {
                    v.Add <FooImpl1>(fooValidator);
                });
            });

            var model = new Root {
                Foo = new FooImpl1()
            };

            var result = validator.Validate(model, options => {
                options.IncludeRuleSets("test").IncludeRulesNotInRuleSet();
            });

            result.IsValid.ShouldBeFalse();
        }
예제 #5
0
        public void Trims_spaces()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });
            validator.RuleSet("Second", () => {
                validator.RuleFor(x => x.Surname).NotNull();
            });

            var result = validator.Validate(new Person(), v => v.IncludeRuleSets("First", "Second"));

            result.Errors.Count.ShouldEqual(2);
            AssertExecuted(result, "First", "Second");
        }
        public async Task Validates_ruleset_async()
        {
            var validator      = new InlineValidator <Root>();
            var impl1Validator = new InlineValidator <FooImpl1>();
            var impl2Validator = new InlineValidator <FooImpl2>();

            impl1Validator.RuleFor(x => x.Name).Equal("Foo");
            impl1Validator.RuleSet("RuleSet1", () => {
                impl1Validator.RuleFor(x => x.Name).MustAsync((s, token) => Task.FromResult(s != null));
            });


            impl2Validator.RuleFor(x => x.Number).Equal(42);
            impl2Validator.RuleSet("RuleSet2", () => {
                impl2Validator.RuleFor(x => x.Number).MustAsync((i, token) => Task.FromResult(i > 0));
            });

            validator.RuleFor(x => x.Foo).SetInheritanceValidator(v => {
                v.Add(impl1Validator, "RuleSet1")
                .Add(impl2Validator, "RuleSet2");
            });

            var result = await validator.ValidateAsync(new Root { Foo = new FooImpl1() });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Foo.Name");

            result = await validator.ValidateAsync(new Root { Foo = new FooImpl2() });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Foo.Number");
        }
예제 #7
0
        public void Applies_multiple_rulesets_to_rule()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First, Second", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });

            var result = validator.Validate(new Person(), ruleSet: "First");

            result.Errors.Count.ShouldEqual(1);
            AssertExecuted(result, "First");

            result = validator.Validate(new Person(), ruleSet: "Second");
            result.Errors.Count.ShouldEqual(1);
            AssertExecuted(result, "Second");

            result = validator.Validate(new Person(), ruleSet: "Third");
            result.Errors.Count.ShouldEqual(0);
            AssertExecuted(result);

            result = validator.Validate(new Person());
            result.Errors.Count.ShouldEqual(0);
            AssertExecuted(result, "default");
        }
        public void Validates_ruleset()
        {
            var validator      = new InlineValidator <Root>();
            var impl1Validator = new InlineValidator <FooImpl1>();
            var impl2Validator = new InlineValidator <FooImpl2>();

            impl1Validator.RuleFor(x => x.Name).Equal("Foo");
            impl1Validator.RuleSet("RuleSet1", () => {
                impl1Validator.RuleFor(x => x.Name).NotNull();
            });


            impl2Validator.RuleFor(x => x.Number).Equal(42);
            impl2Validator.RuleSet("RuleSet2", () => {
                impl2Validator.RuleFor(x => x.Number).GreaterThan(0);
            });

            validator.RuleFor(x => x.Foo).SetInheritanceValidator(v => {
                v.Add(impl1Validator, "RuleSet1")
                .Add(impl2Validator, "RuleSet2");
            });

            var result = validator.Validate(new Root {
                Foo = new FooImpl1()
            });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Foo.Name");

            result = validator.Validate(new Root {
                Foo = new FooImpl2()
            });
            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Foo.Number");
        }
예제 #9
0
        public void Trims_spaces()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });
            validator.RuleSet("Second", () => {
                validator.RuleFor(x => x.Surname).NotNull();
            });

#pragma warning disable 618
            var result = validator.Validate(new Person(), ruleSet: "First, Second");
#pragma warning restore 618
            result.Errors.Count.ShouldEqual(2);
            AssertExecuted(result, "First", "Second");
        }
예제 #10
0
        public void Includes_all_rulesets()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Forename).NotNull();
            validator.RuleSet("Test1", () => {
                validator.RuleFor(x => x.Surname).NotNull();
            });
            validator.RuleSet("Test2", () => {
                validator.RuleFor(x => x.Age).GreaterThan(0);
            });

            var result = validator.Validate(new Person(), options => {
                options.IncludeAllRuleSets();
            });

            result.Errors.Count.ShouldEqual(3);
        }
예제 #11
0
        public async Task Includes_all_rulesets_async()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Forename).MustAsync((x, t) => Task.FromResult(x != null));
            validator.RuleSet("Test1", () => {
                validator.RuleFor(x => x.Surname).MustAsync((x, t) => Task.FromResult(x != null));
            });
            validator.RuleSet("Test2", () => {
                validator.RuleFor(x => x.Age).MustAsync((x, t) => Task.FromResult(x > 0));
            });

            var result = await validator.ValidateAsync(new Person(), options => {
                options.IncludeAllRuleSets();
            });

            result.Errors.Count.ShouldEqual(3);
        }
        public void New_CustomAsync_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => {
                validator.RuleFor(x => x).CustomAsync(async(x, ctx, cancel) => {
                    ctx.AddFailure("x", "y");
                });
            });

            validator.RuleSet("bar", () => {
                validator.RuleFor(x => x).CustomAsync(async(x, ctx, cancel) => {
                    ctx.AddFailure("x", "y");
                });
            });

            var result = validator.ValidateAsync(new Person(), ruleSet: "foo").Result;

            result.Errors.Count.ShouldEqual(1);
        }
        public void New_Custom_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => {
                validator.RuleFor(x => x).Custom((x, ctx) => {
                    ctx.AddFailure("x", "y");
                });
            });

            validator.RuleSet("bar", () => {
                validator.RuleFor(x => x).Custom((x, ctx) => {
                    ctx.AddFailure("x", "y");
                });
            });

            var result = validator.Validate(new Person(), v => v.IncludeRuleSets("foo"));

            result.Errors.Count.ShouldEqual(1);
        }
예제 #14
0
        public async Task Includes_combination_of_rulesets_async()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Forename).MustAsync((x, t) => Task.FromResult(x != null));
            validator.RuleSet("Test1", () => {
                validator.RuleFor(x => x.Surname).MustAsync((x, t) => Task.FromResult(x != null));
            });
            validator.RuleSet("Test2", () => {
                validator.RuleFor(x => x.Age).MustAsync((x, t) => Task.FromResult(x > 0));
            });

            var result = await validator.ValidateAsync(new Person(), options => {
                options.IncludeRuleSets("Test1").IncludeRulesNotInRuleSet();
            });

            result.Errors.Count.ShouldEqual(2);
            result.Errors[0].PropertyName.ShouldEqual("Forename");
            result.Errors[1].PropertyName.ShouldEqual("Surname");
        }
예제 #15
0
        public void Includes_combination_of_rulesets()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Forename).NotNull();
            validator.RuleSet("Test1", () => {
                validator.RuleFor(x => x.Surname).NotNull();
            });
            validator.RuleSet("Test2", () => {
                validator.RuleFor(x => x.Age).GreaterThan(0);
            });

            var result = validator.Validate(new Person(), options => {
                options.IncludeRuleSets("Test1").IncludeRulesNotInRuleSet();
            });

            result.Errors.Count.ShouldEqual(2);
            result.Errors[0].PropertyName.ShouldEqual("Forename");
            result.Errors[1].PropertyName.ShouldEqual("Surname");
        }
예제 #16
0
        public void New_Custom_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => {
                validator.RuleFor(x => x).Custom((x, ctx) => {
                    ctx.AddFailure("x", "y");
                });
            });

            validator.RuleSet("bar", () => {
                validator.RuleFor(x => x).Custom((x, ctx) => {
                    ctx.AddFailure("x", "y");
                });
            });

#pragma warning disable 618
            var result = validator.Validate(new Person(), ruleSet: "foo");
#pragma warning restore 618
            result.Errors.Count.ShouldEqual(1);
        }
        public async Task New_CustomAsync_within_ruleset()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("foo", () => {
                validator.RuleFor(x => x).CustomAsync((x, ctx, cancel) => {
                    ctx.AddFailure("x", "y");
                    return(Task.CompletedTask);
                });
            });

            validator.RuleSet("bar", () => {
                validator.RuleFor(x => x).CustomAsync((x, ctx, cancel) => {
                    ctx.AddFailure("x", "y");
                    return(Task.CompletedTask);
                });
            });

            var result = await validator.ValidateAsync(new Person(), v => v.IncludeRuleSets("foo"));

            result.Errors.Count.ShouldEqual(1);
        }
예제 #18
0
        public void Executes_in_rule_in_default_and_none()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First, Default", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });
            validator.RuleFor(x => x.Forename).NotNull();

            var result = validator.Validate(new Person(), ruleSet: "default");

            result.Errors.Count.ShouldEqual(2);
        }
예제 #19
0
        public void Executes_in_rule_in_default_and_none()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First, Default", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });
            validator.RuleFor(x => x.Forename).NotNull();

#pragma warning disable 618
            var result = validator.Validate(new Person(), ruleSet: "default");
#pragma warning restore 618
            result.Errors.Count.ShouldEqual(2);
            AssertExecuted(result, "default");
        }
예제 #20
0
        public void Uses_explicit_ruleset()
        {
            var addressValidator = new InlineValidator <Address>();

            addressValidator.RuleSet("ruleset1", () => {
                addressValidator.RuleFor(x => x.Line1).NotNull();
            });
            addressValidator.RuleFor(x => x.Line2).NotNull();
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Address).SetValidator(addressValidator, ruleSets: "ruleset1");

            var result = validator.Validate(new Person {
                Address = new Address()
            });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Address.Line1");
        }
예제 #21
0
        public async Task Combines_rulesets_and_explicit_properties_async()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleFor(x => x.Forename).NotNull();
            validator.RuleFor(x => x.Surname).NotNull();
            validator.RuleSet("Test", () => {
                validator.RuleFor(x => x.Age).GreaterThan(0);
            });

            var result = await validator.ValidateAsync(new Person(), options => {
                options.IncludeRuleSets("Test");
                options.IncludeProperties(x => x.Forename);
            });

            result.Errors.Count.ShouldEqual(2);
            result.Errors[0].PropertyName.ShouldEqual("Forename");
            result.Errors[1].PropertyName.ShouldEqual("Age");
        }
예제 #22
0
        public void Executes_in_rule_in_ruleset_and_default()
        {
            var validator = new InlineValidator <Person>();

            validator.RuleSet("First, Default", () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });

            var result = validator.Validate(new Person(), ruleSet: "First");

            result.Errors.Count.ShouldEqual(1);
            AssertExecuted(result, "First");

            result = validator.Validate(new Person(), ruleSet: "Second");
            result.Errors.Count.ShouldEqual(0);
            AssertExecuted(result);

            result = validator.Validate(new Person());
            result.Errors.Count.ShouldEqual(1);
            AssertExecuted(result, "default");
        }
예제 #23
0
        public void Ruleset_cascades_to_child_validator()
        {
            var addressValidator = new InlineValidator <Address>();

            addressValidator.RuleSet("Test", () => {
                addressValidator.RuleFor(x => x.Line1).NotNull();
            });

            var validator = new TestValidator();

            validator.RuleSet("Test", () => {
                validator.RuleFor(x => x.Address).SetValidator(addressValidator);
            });

            var person = new Person {
                Address = new Address()
            };

            var result = validator.Validate(new ValidationContext <Person>(person, new PropertyChain(), new RulesetValidatorSelector("Test")));

            result.Errors.Count.ShouldEqual(1);
        }
예제 #24
0
        public void Ruleset_cascades_to_child_collection_validator()
        {
            var orderValidator = new InlineValidator <Order>();

            orderValidator.RuleSet("Test", () => {
                orderValidator.RuleFor(x => x.ProductName).NotNull();
            });

            var validator = new TestValidator();

            validator.RuleSet("Test", () => {
                validator.RuleFor(x => x.Orders).SetValidator(orderValidator);
            });

            var person = new Person {
                Orders = { new Order(), new Order() }
            };

            var result = validator.Validate(new ValidationContext <Person>(person, new PropertyChain(), new RulesetValidatorSelector("Test")));


            result.Errors.Count.ShouldEqual(2);             //one for each order
        }