Expose FluentValidation configuration through UseFluentValidation overload#2497
Conversation
…rload Add FluentValidationConfiguration class that exposes both RegistrationBehavior and direct access to ValidatorOptions.Global (cascade modes, severity, language manager, property name resolvers, etc.). Add new UseFluentValidation(Action<>) overload that is fully backward-compatible with the existing API.
Use FluentValidation's own AssemblyScanner when IncludeInternalTypes is enabled, since Lamar's ConnectImplementationsToTypesClosing only finds public types. Preserves the existing lifetime logic (Scoped for validators with constructor dependencies, Singleton otherwise).
|
Just want to say that I usually mark my FV validators as internal sealed because I don’t consider them as part of the public API, and FV does support it OOB. But maybe it’s a bit opiniated for Wolverine, not sure. |
So, I think the But, I'd also say that if you have a Fluent Validation validator that requires access to IoC services, then it shouldn't be done w/ FV and you should use a Wolverine Did you forget to check in a markdown change? I see you have a code sample for the docs, but no doc changes in the PR. |
No no I agree that validators shouldn’t require injection. |
Summary
FluentValidationConfigurationclass that exposesRegistrationBehavior,IncludeInternalTypes, and direct access toValidatorOptions.Global(cascade modes, severity, language manager, property name resolvers, etc.)UseFluentValidation(Action<FluentValidationConfiguration>)overload — fully backward-compatible with the existing APIIncludeInternalTypesoption that uses FluentValidation's ownAssemblyScannerto discoverinternalvalidators, since Lamar'sConnectImplementationsToTypesClosingonly finds public typesMotivation
Two gaps in the current FluentValidation extension:
No access to FluentValidation configuration — users cannot configure
ValidatorOptions.Global(cascade modes, severity, language manager, etc.) through the Wolverine API. They must set these separately.Internal validators are not discovered — Lamar's assembly scanning uses
Assembly.GetExportedTypes()which only returns public types. FluentValidation's ownAssemblyScannersupports anincludeInternalTypesparameter.This change lets users configure everything in one place:
Test plan
configure_validator_options_via_action_overload— verifies global options are appliedconfigure_registration_behavior_via_action_overload— verifies explicit registration through new overloadaction_overload_still_applies_middleware— verifies middleware is still wired updiscover_internal_validators_when_include_internal_types_is_true— internal validator is registered as Singletondo_not_discover_internal_validators_by_default— internal validators are NOT found without the flagdiscover_internal_validator_with_dependencies_as_scoped— internal validator with ctor deps is Scopedfluent_validation_configuration_defaults— verifies default values