You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
The text was updated successfully, but these errors were encountered:
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.
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:
Expected: all items in
totest
should validate false, Actually: the second item is not validate false but true;The text was updated successfully, but these errors were encountered: