getoxs / mediatr.extensions.fluentvalidation.aspnetcore Goto Github PK
View Code? Open in Web Editor NEWMediatR extension to FluentValidation for .NET framework
License: MIT License
MediatR extension to FluentValidation for .NET framework
License: MIT License
Using exceptions for flow control is very "heavy".
Anyone figure out how to return a result with errors, instead of throwing exceptions?
To be able to use .net 5 RC1
Is this Possible?
Thanks
Packages installed:
var applicationAssembly = typeof(CreateUserValidator).Assembly;
services.AddMediatR(applicationAssembly);
services.AddFluentValidation(new []{ applicationAssembly });
System.MissingMethodException: Method not found: 'FluentValidation.AssemblyScanner FluentValidation.AssemblyScanner.FindValidatorsInAssembly(System.Reflection.Assembly)'.
at FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssembly(IServiceCollection services, Assembly assembly, ServiceLifetime lifetime, Func`2 filter)
at FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssemblies(IServiceCollection services, IEnumerable`1 assemblies, ServiceLifetime lifetime, Func`2 filter) in C:\Projects\FluentValidation\src\FluentValidation.DependencyInjectionExtensions\ServiceCollectionExtensions.cs:line 36
at MediatR.Extensions.FluentValidation.AspNetCore.ServiceCollectionExtensions.AddFluentValidation(IServiceCollection services, IEnumerable`1 assemblies, ServiceLifetime lifetime, Func`2 filter)
at ...
Hi, first of all thank you for your work, that package is so usefull.
Now the problem.
I'm trying to validate a collection of DTOs with RuleForEach and setting a validator of another class with SetValidator and is not detecting the validation.
If I try to validate inside the Handler manually is working perfect.
When using the extension method to register the validators:
builder.Services.AddFluentValidation(new[] { assembly });
It misses the parameter includeInternalTypes
for services.AddValidatorsFromAssemblies
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddFluentValidation(this IServiceCollection services,
IEnumerable<Assembly> assemblies,
ServiceLifetime lifetime = ServiceLifetime.Transient,
Func<AssemblyScanner.AssemblyScanResult, bool>? filter = null,
bool includeInternalTypes = false)
{
services.Add(new ServiceDescriptor(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>), lifetime));
services.AddValidatorsFromAssemblies(assemblies, lifetime, filter, includeInternalTypes);
return services;
}
}
MediatR.Extensions.FluentValidation.AspNetCoreversion version 1.1.1 is not compatible with FluentValidation 10.1.0 version.
Any chance to upgrade the packages within the solution?
Unhandled exception. System.MissingMethodException: Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssemblies(Microsoft.Extensions.DependencyInjection.IServiceCollection, System.Collections.Generic.IEnumerable`1<Sy
stem.Reflection.Assembly>, Microsoft.Extensions.DependencyInjection.ServiceLifetime)'.
at MediatR.Extensions.FluentValidation.AspNetCore.ServiceCollectionExtensions.AddFluentValidation(IServiceCollection services, IEnumerable`1 assemblies, ServiceLifetime lifetime)
When I copied code directly - it worked.
Hello,
It seems that the update to MediatR 10 breaks this extension, probably because the contracts were moved to another assembly.
Is it possible to update the lib to handle this new version?
Describe the bug
Validation does not work for IRequest
(not generic)
To Reproduce
Steps to reproduce the behavior:
using MediatR;
public sealed class Command : IRequest
{
public string Name { get; set; }
}
public sealed class CommandValidator : AbstractValidator<Command> {
RuleFor(x => x.Name).NotEmpty();
}
Send mediatr request with this command and Name = null.
Expected behavior
Validation does not work.
Library (please complete the following information):
Additional context
You need change IRequest<TResponse>
to IBaseRequest
as like
public sealed class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : class, IBaseRequest
MediatR.Extensions.FluentValidation.AspNetCore is broken with FluentValidation version 11. It worked fine in FluentValidation version 10. However, after upgrading to 11.0.1, the application breaks on start throwing the following exception from service.AddFluentValidation:
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection FluentValidation.ServiceCollectionExtensions.AddValidatorsFromAssemblies(Microsoft.Extensions.DependencyInjection.IServiceCollection, System.Collections.Generic.IEnumerable1<System.Reflection.Assembly>, Microsoft.Extensions.DependencyInjection.ServiceLifetime, System.Func2<AssemblyScanResult,Boolean>)'.
Source=MediatR.Extensions.FluentValidation.AspNetCore
StackTrace:
at MediatR.Extensions.FluentValidation.AspNetCore.ServiceCollectionExtensions.AddFluentValidation(IServiceCollection services, IEnumerable1 assemblies, ServiceLifetime lifetime, Func2 filter)
at Api.Startup.ConfigureServices(IServiceCollection services) in C:[project folder omitted]\Startup.cs:line 100
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.g__Startup|0(IServiceCollection serviceCollection)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.b__0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, Object instance)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__0(HostBuilderContext context, IServiceCollection services)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Api.Program.Main(String[] args) in C:[project folder omitted]\Program.cs:line 12
I opened a bug with FluentValidation, but they closed it saying this is a problem with MediatR.Extensions.FluentValidation.AspNetCore. You can see that issue here for reference: FluentValidation/FluentValidation#1939
They say you will "need to publish a new version of this package that’s compatible with FluentValidation 11 (they should just need to recompile targeting FV 11). They should also ensure that they lock their dependency versions in line with SemVer to not auto upgrade across major versions to prevent issues like this in the future."
Is your feature request related to a problem? Please describe.
Until now FluentValidation, async rules were silently run as synchronous, so they could be used.
Since v11, it now throws an exception when an async rule is executed synchronously, a lot of our validators are now throwing exceptions when validating a command.
Describe the solution you'd like
Very very quickly looking at the code, it looks like the Validate() is already running in an async context.
So it should be possible to call ValidateAsync instead of Validate, allowing to use async rules without too much impact.
Could it be possible to change the code to do that ?
Hello,
Is it possible to use RuleSet for validation?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.