OpenAPI: RuleForEach() validators are applied to the collection instead of its items
FastEndpoints.OpenApi (current main), FluentValidation 12.1.1, .NET 10.
Validators added with RuleForEach() end up on the array property itself instead of on its items schema.
public sealed class Request
{
public List<string>? Tags { get; set; }
}
public sealed class RequestValidator : Validator<Request>
{
public RequestValidator()
{
RuleForEach(x => x.Tags).NotEmpty().MaximumLength(20);
}
}
Actual (trimmed):
"required": ["tags"],
"properties": {
"tags": { "type": "array", "maxItems": 20, "minItems": 0, "items": { "type": "string" } }
}
Expected:
"properties": {
"tags": { "type": ["null", "array"], "items": { "type": "string", "minLength": 1, "maxLength": 20 } }
}
So the document says tags is required, can't be null and can have at most 20 entries, while the validator accepts a missing or empty list and only limits the length of each tag. Same thing happens with numeric items, e.g. RuleForEach(x => x.Scores).InclusiveBetween(1, 5) puts minimum/maximum on the array.
RuleForEach().SetValidator() / ChildRules() already go to the items schema through ChildValidatorResolver, it's only the plain property validators that don't. The rule dictionary keys RuleForEach(x => x.Tags) as tags, and ValidationSchemaApplier.ApplyValidationRule doesn't check whether the rule is an ICollectionRule<,>.
I have a fix with tests and can open a PR.