Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChildRules do not seem to be validated on specific ruleset #1597

Closed
hvanzelst opened this issue Dec 14, 2020 · 2 comments
Closed

ChildRules do not seem to be validated on specific ruleset #1597

hvanzelst opened this issue Dec 14, 2020 · 2 comments
Milestone

Comments

@hvanzelst
Copy link

When creating chlidRules, the childrules do not validate when calling the validate on the ruleset. See the code below, the expected result would be that all three Foo instances are validated to be false, however the middle one is validated to be true, even though remark is not filled. It does seem to be working when validating AllRuleSets, however we have rulesets for Create and Update seperate and do not want to validate them all. It also works when setting a validtor using SetValidator() within the RuleForEach, this solves the issue for now, but it is not the way I would like to write this specific validator. Check out the code and tell me what you think please:

internal class Program
    {
        private static void Main(string[] args)
        {
            List<Foo> totest = new List<Foo>
            {
                new Foo
                {
                    Description = null,
                    Bars = new List<Bar>() { new Bar { Remark = "Remark" } }
                },
                new Foo
                {
                    Description = "Description",
                    Bars = new List<Bar>() { new Bar { Remark = null } }
                },
                new Foo
                {
                    Description = null,
                    Bars = new List<Bar>() { new Bar { Remark = null } }
                }
            };

            Validator validator = new Validator();

            foreach (var foo in totest)
            {
                var result = validator.Validate(foo, options => options.IncludeRuleSets("testing"));
                Console.WriteLine($"{result.IsValid} - {string.Join("", result.Errors.Select(e => e.ErrorMessage)) }");
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }

    public class Foo
    {
        public string Description { get; set; }

        public IList<Bar> Bars { get; set; }
    }

    public class Bar
    {
        public string Remark { get; set; }
    }

    public class Validator : AbstractValidator<Foo>
    {
        public Validator()
        {
            RuleSet("testing", () =>
            {
                RuleFor(a => a.Description).NotEmpty().MaximumLength(100);
                RuleForEach(a => a.Bars)
                .ChildRules(child =>
                {
                    child.RuleFor(b => b.Remark).NotEmpty().MaximumLength(100);
                });
            });
        }
    }

Expected: all items in totest should validate false, Actually: the second item is not validate false but true;

@JeremySkinner
Copy link
Member

Looks like this is probably a bug, child rules should indeed work the same way as root-level rules when wrapped inside a ruleset. I don't personally have time to dig into this at the moment due to other commitments, but will aim to do so sometime after Christmas. In the mean time if you'd like to have a go at submitting a PR that resolves this for it then that'd be very welcome, but otherwise I'll try and get to it when I can.

@JeremySkinner
Copy link
Member

This is fixed and will be part of the 9.4 release

@JeremySkinner JeremySkinner added this to the 9.4 milestone Jan 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants