fluentvalidation

Using FluentValidator to validate children of properties

不羁岁月 提交于 2019-12-23 09:29:31
问题 I want to use FluentValidation to validate some classes one of which is only used as a property on another... but I never directly create the child class so I want to test validation from the parent level. This may be unnecessary / crazy So for example I have public class Parent { public string Text {get;set;} public Child Child {get;set;} } public class Child { public string Text {get;set;} } and public class ParentValidator : AbstractValidator<Parent> { public ParentValidator() { RuleFor(p=

FluentValidation, Validators lifecycle and using Validators to check against Db

≯℡__Kan透↙ 提交于 2019-12-22 04:44:22
问题 I'm using FluentValidation in my MVC4 project. Every thing works perfectly and it's connected to my IoC ( StructureMap ). I have 2 questions: How should I manage lifecycle of my validators ? Is it ok to make them singleton ? or it makes no difference and I can manage lifecycle according to my needs ? What is the best practice here? FluentValidation is very good. I have been using it for simple validations (like: property is not empty, etc.). I'm thinking about doing some Db validations using

Override default ASP.NET MVC message in FluentValidation

痞子三分冷 提交于 2019-12-21 12:57:55
问题 I get the validation message "The value xxx is not valid for yyy". It happens when I post incorrect value for double type. I have no idea how to change it. 回答1: Unfortunately this isn't something that FluentValidation has the ability to override - the extensibility model for MVC's validation is somewhat limited in many places, and I've not been able to find a way to override this particular message. An alternative approach you could use is to define two properties on your view model - one as

SimpleInjector and FluentValidationFactory

核能气质少年 提交于 2019-12-21 05:13:12
问题 I am trying to automate the validation of my view models, I know I can just add a an attribute to specify my validation but there is an option to set up a factory to automate all that, I looked at: this answer and came up with this using simple injector 3.1: public class CustomValidatorFactory:ValidatorFactoryBase { private readonly Container siContainer; public CustomValidatorFactory(Container siContainer) { var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); this.siContainer

FluentValidation rule for multiple properties

↘锁芯ラ 提交于 2019-12-21 03:23:19
问题 I have a FluentValidator that has multiple properties like zip and county etc. I want to create a rule that takes two properties just like a RuleFor construct public class FooArgs { public string Zip { get; set; } public System.Guid CountyId { get; set; } } public class FooValidator : AbstractValidator<FooArgs> { RuleFor(m => m.CountyId).Must(ValidZipCounty).WithMessage("wrong Zip County"); } This works but I want to pass both Zip and county to the rue in order to validate. What is the best

ServiceStack - Validation and Database Access

孤人 提交于 2019-12-19 09:31:09
问题 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

ServiceStack - Validation and Database Access

送分小仙女□ 提交于 2019-12-19 09:31:07
问题 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

FluentValidation multiple validators

北战南征 提交于 2019-12-18 12:23:30
问题 Can I add more than one validator to an object? For example: public interface IFoo { int Id { get; set; } string Name { get; set; } } public interface IBar { string Stuff { get; set; } } public class FooValidator : AbstractValidator<IFoo> { public FooValidator () { RuleFor(x => x.Id).NotEmpty().GreaterThan(0); } } public class BarValidator : AbstractValidator<IBar> { public BarValidator() { RuleFor(x => x.Stuff).Length(5, 30); } } public class FooBar : IFoo, IBar { public int Id { get; set; }

FluentValidation - validating across multiple properties

孤街醉人 提交于 2019-12-18 12:13:02
问题 Have a form where a user can enter start date/time and end date/time for an event. Here's the validator so far: public class EventModelValidator : AbstractValidator<EventViewModel> { public EventModelValidator() { RuleFor(x => x.StartDate) .NotEmpty().WithMessage("Date is required!") .Must(BeAValidDate).WithMessage("Invalid date"); RuleFor(x => x.StartTime) .NotEmpty().WithMessage("Start time is required!") .Must(BeAValidTime).WithMessage("Invalid Start time"); RuleFor(x => x.EndTime)

C# FluentValidation for a hierarchy of classes

醉酒当歌 提交于 2019-12-18 10:28:26
问题 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? 回答1: One approach to take would be as follows: public class Base { public string BaseName { get; set; } } public class Derived1 :