Skip to content

Not taking into account childRules when they point to the same property - #235

Merged
avgalex merged 2 commits into
micro-elements:masterfrom
jgarciadelanoceda:ChildRulesNotApplied
Oct 31, 2026
Merged

avgalex merged 2 commits into
micro-elements:masterfrom
jgarciadelanoceda:ChildRulesNotApplied

Conversation

@jgarciadelanoceda

@jgarciadelanoceda jgarciadelanoceda commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes missing OpenAPI constraints when a property uses ChildRules with an identity rule such as:

ruleBuilder.ChildRules(child =>
    child.RuleFor(x => x)
        .Matches(pattern)
        .DependentRules(() =>
            child.RuleFor(x => x).Must(CountryExists)));

Previously, the library did not associate the identity child rule with its owning property, so the regex pattern was missing from the generated schema.
Changes
• Apply identity child-rule constraints to the owning property or collection item.
• Support nested identity validators while preventing recursive traversal.
• Respect existing rule filters and conditional-rule settings.
• Preserve parent constraints when a runtime-dependent child validator cannot be resolved.
• Update the changelog and add regression tests.
Runtime validation remains unchanged. Extensions can continue accepting IRuleBuilder<T, string> anywhere in the pipeline, and schema generation does not execute Must predicates or repository lookups.
Validation
• All 514 tests passed across supported target frameworks.
• Solution build succeeded.ç

@avgalex. I have tested on my own machine with a local nuget and it works for my scenario.

I have changed an App from CascadeMode.Stop to Continue globally and an extension method I had does 2 validations, the first is almost Nop(Regex) check. The second one is a call to a repository

@jgarciadelanoceda jgarciadelanoceda changed the title Not taking into account childRules Not taking into account childRules when they point to the same property Oct 30, 2026
Do not promote NotNull and NotEmpty inside null-skipping child validators
into required parent properties. Keep non-empty length constraints and
explicit parent presence rules across all three OpenAPI backends.

Add regression coverage for nullable properties, arrays, collection items,
nested identity rules, and stronger parent length constraints. Prepare
version 7.2.3 and update the changelog.

Validation: 569 tests passed in Release across supported target frameworks;
Release solution build and four 7.2.3 packages verified locally.
@avgalex

avgalex commented Oct 31, 2026

Copy link
Copy Markdown
Member

Thanks for the fix and for testing it in your application!

During review, I found one edge case: FluentValidation skips child validators when the parent value is null, but propagating an inner NotNull or NotEmpty made that parent property required in OpenAPI.

I pushed a follow-up fix to this PR. It preserves nullable/optional properties while retaining minLength/minItems constraints. Explicit NotNull/NotEmpty rules on the parent still work. I also adjusted NSwag handling so stronger parent length constraints are preserved regardless of rule order.

Added regression coverage across all three OpenAPI backends. All 569 tests pass locally, including 55 new checks. The version and changelog are prepared for 7.2.3.

@avgalex
avgalex merged commit 841c80e into micro-elements:master Oct 31, 2026
2 checks passed
@jgarciadelanoceda
jgarciadelanoceda deleted the ChildRulesNotApplied branch October 31, 2026 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants