fluentvalidation

Add validation to a MediatR behavior pipeline?

隐身守侯 提交于 2019-12-02 21:35:46
I'm using ASP.NET Core, the built-in container, and MediatR 3 which supports "behavior" pipelines : public class MyRequest : IRequest<string> { // ... } public class MyRequestHandler : IRequestHandler<MyRequest, string> { public string Handle(MyRequest message) { return "Hello!"; } } public class MyPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> { public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next) { var response = await next(); return response; } } // in `Startup.ConfigureServices()`: services.AddTransient(typeof

C# FluentValidation for a hierarchy of classes

早过忘川 提交于 2019-12-02 16:33:02
I have a hierarchy of data classes public class Base { // Fields to be validated } public class Derived1 : Base { // More fields to be validated } public class Derived2 : Base { // More fields to be validated } What would be the appropriate way to validated Derived1 and Derived2 using FluentValidation framework without duplicating rules for fields of Base class? One approach to take would be as follows: public class Base { public string BaseName { get; set; } } public class Derived1 : Base { public string Derived1Name { get; set; } } public class BaseValidator<T> : AbstractValidator<T> where T

Custom validation attribute with multiple instances problem

元气小坏坏 提交于 2019-12-01 17:31:23
问题 I'm using tha namespace System.ComponentModel.DataAnnotations in C# 4 to implement my own validation attribute and it looks like this [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public sealed class MyCustomValidator : ValidationAttribute { private String Property1 { get; set; } private String Property2 { get; set; } public ValeTaxiSituacaoRequired(String property1, String property2) { Property1 = property1; Property2 = property2; } public override bool IsValid(object value)

Fluent Validation in MVC: specify RuleSet for Client-Side validation

≯℡__Kan透↙ 提交于 2019-12-01 17:14:50
In my ASP.NET MVC 4 project I have validator for one of my view models, that contain rules definition for RuleSets. Edit ruleset used in Post action, when all client validation passed. Url and Email rule sets rules used in Edit ruleset (you can see it below) and in special ajax actions that validate only Email and only Url accordingly. My problem is that view doesn't know that it should use Edit rule set for client html attributes generation, and use default rule set, which is empty. How can I tell view to use Edit rule set for input attributes generation? Model: public class ShopInfoViewModel

FluentValidation NotEmpty and EmailAddress example

好久不见. 提交于 2019-12-01 16:04:29
I am using FluentValidation with a login form. The email address field is Required and Must be a valid email address . I want to display a custom error message in both cases. The code I have working is: RuleFor(customer => customer.email) .NotEmpty() .WithMessage("Email address is required."); RuleFor(customer => customer.email) .EmailAddress() .WithMessage("A valid email address is required."); The above code does work and shows (2) different error messages. Is there a better way of writing the multiple error message for one field? UPDATE - WORKING Chaining and add .WithMessage after each

FluentValidation NotEmpty and EmailAddress example

ぃ、小莉子 提交于 2019-12-01 15:25:07
问题 I am using FluentValidation with a login form. The email address field is Required and Must be a valid email address . I want to display a custom error message in both cases. The code I have working is: RuleFor(customer => customer.email) .NotEmpty() .WithMessage("Email address is required."); RuleFor(customer => customer.email) .EmailAddress() .WithMessage("A valid email address is required."); The above code does work and shows (2) different error messages. Is there a better way of writing

Fluent Validation, different validation for each item in a list in Asp.NET Core

懵懂的女人 提交于 2019-12-01 11:26:38
I´ve been trying to find a way to validate items inside a list, each with different validation rules. I came upon Fluent validation which is a great library but I can´t seem to find a way to do validation for each item individually. I got a faint idea from this similar thread ( Validate 2 list using fluent validation ), but I´m not sure how to focus it how I want. So I have this View Model: public class EditPersonalInfoViewModel { public IList<Property> UserPropertyList { get; set; } } This contains a list of Active Directory properties. Each represented by this class: public class Property {

Adding DataAnnotation to class when using FluentValidation

微笑、不失礼 提交于 2019-12-01 11:17:45
I use the FluentValidation framework to add validation and annotations to my models in an MVC project. I need to add data annotations to the class level of a model. Namely, the model needs to have the DisplayColumn attribute added. But, since I use FluentValidation (and have the application's ModelMetadataProvider set to use FluentValidation), even if I put the DisplayColumn attribute on the model class, it isn't used. However, I can't find a way to add that annotation by using FluentValidation. Does anyone have any idea how I can get that to work? Thanks I wouldn't actually recommend using

ServiceStack - Validation and Database Access

不羁的心 提交于 2019-12-01 09:26:04
I'm implementing an Api with ServiceStack. One of the key aspects of my solution is an aggressive validation strategy. I use ServiceStack's ValidationFeature, meaning that if there is an IValidator< ReqDto > (or its descendant: AbstractValidator< ReqDto >) registered in the app container, validation will automatically run prior to the service. By aggressive validation, what I mean is that I check all possible error scenarios, and logic validations at the validator level. As a result my service logic is extremely clean and short. This independance of Service Logic from Service Validation is

Fluent Validation, different validation for each item in a list in Asp.NET Core

╄→гoц情女王★ 提交于 2019-12-01 08:29:20
问题 I´ve been trying to find a way to validate items inside a list, each with different validation rules. I came upon Fluent validation which is a great library but I can´t seem to find a way to do validation for each item individually. I got a faint idea from this similar thread (Validate 2 list using fluent validation), but I´m not sure how to focus it how I want. So I have this View Model: public class EditPersonalInfoViewModel { public IList<Property> UserPropertyList { get; set; } } This