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
The FluentValidationValidator validates the Postcode field using the AddressValidator, even though the CompanyValidator for the Model does not reference the AddressValidator, and companyValidator.Validate() does not use the AddressValidator. The Postcode field should not be validated in this case.
Expected behavior
The validator for the Model (CustomerValidator) should be used for all field validations, company.Address.Postcode should not be validated as CustomerValidator does not reference AddressValidator.
I believe the issue is in how the ValidateField() method is implemented. It uses fieldIdentifier.Model directly and does not take editContext.Model into account. The implementation won’t be trivial (you cannot simply replace fieldIdentifier.Model with editContext.Model as someone has already tried), but it is doable.
Describe the bug
Assume we have a complex type from this documentation: https://docs.fluentvalidation.net/en/latest/start.html#complex-properties
There is a
Customer
class with anAddress
property and two validators:CustomerValidator
andAddressValidator
.Let’s remove this line from the
CustomerValidator
:public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { RuleFor(customer => customer.Name).NotNull(); - RuleFor(customer => customer.Address).SetValidator(new AddressValidator()); } }
The
AddressValidator
still exists:Now, when you create this type of UI (shortened for brevity):
The
FluentValidationValidator
validates thePostcode
field using theAddressValidator
, even though theCompanyValidator
for theModel
does not reference theAddressValidator
, andcompanyValidator.Validate()
does not use theAddressValidator
. ThePostcode
field should not be validated in this case.Expected behavior
The validator for the
Model
(CustomerValidator
) should be used for all field validations,company.Address.Postcode
should not be validated asCustomerValidator
does not referenceAddressValidator
.Additional context
FluentValidation/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs
Lines 84 to 105 in f02c9be
I believe the issue is in how the
ValidateField()
method is implemented. It usesfieldIdentifier.Model
directly and does not takeeditContext.Model
into account. The implementation won’t be trivial (you cannot simply replacefieldIdentifier.Model
witheditContext.Model
as someone has already tried), but it is doable.Related items:
cc @jirikanda
The text was updated successfully, but these errors were encountered: