fluentvalidation

FluentValidation ModelState.IsValid always true

匆匆过客 提交于 2019-12-11 03:36:48
问题 Ok my problem is the modelvalidator from fluentValidation is not working in my project, and ModelState.IsValid is always true no matter the state of the validation, im using asp.net mvc 4, .net 4.5, thx in advance. Global.asax protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles

FluentValidation LogOnFailure override

折月煮酒 提交于 2019-12-11 02:48:28
问题 Inside my validator class I have couple of rules. I need to log to database some validation errors. Here are my validators: RuleFor(u => u.LastName) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty().WithMessage("Last name is required") .Length(3, 20).WithMessage("Must have between 3 and 20 letters"); RuleFor(u => u.BirthDate) .Cascade(CascadeMode.StopOnFirstFailure) .NotNull().WithMessage("Birth date is required") .Must(c => c > new DateTime(1920, 01, 01)).WithMessage("Too old"); RuleFor(u

FluentValidation collection properties not validated

十年热恋 提交于 2019-12-10 18:28:08
问题 This is the first time I'm trying to implement FluentValidation since I need to cover a complex validation scenario. The class I'm trying to validate has a large quantity of properties, complex objects and several collections. I didn't have troubles to validate properties of the main class or even checking if collections are not empty, but I do have problems while validating objects properties within each collection. To implement this I followed the examples documented here (check under "Re

Issues setting up FluentValidation with Castle.Windsor

谁都会走 提交于 2019-12-10 17:17:50
问题 I have an asp.net MVC 4.5 application with Castle.Windsor 3.2.2 as DI and I'm trying to add FluentValidation version 5.0.0.1 for the first time. I created the factory inheriting from ValidatorFactoryBase public class WindsorFluentValidatorFactory : ValidatorFactoryBase { private readonly IKernel _kernel; public WindsorFluentValidatorFactory(IKernel kernel) { _kernel = kernel; } public override IValidator CreateInstance(Type validatorType) { return _kernel.HasComponent(validatorType) ? _kernel

FluentValidation: How do i put all validation messages at a single location?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 13:23:51
问题 This is one of my validation class: public class StocksValidator : AbstractValidator<Stocks> { public StocksValidator() { RuleFor(x => x.SellerId).GreaterThan(1).WithMessage("SellerId should be greater than 1") .LessThan(100).WithMessage("SellerId should be less than 100"); RuleFor(x => x.SellerType).GreaterThan(101).WithMessage("SellerType should be greater than 101") .LessThan(200).WithMessage("SellerType should be less than 200"); RuleFor(x => x.SourceId).GreaterThan(201).WithMessage(

Per-Request DependencyResolver in Web API

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 12:54:58
问题 In MVC, a ModelValidatorProvider is instantiated and called to validate a model on each request. This means that in a DI environment, it can take dependencies on objects scoped within a single request, such as a Unit of Work or Database context. In Web API, this appears to have been significantly changed. Instead of being instantiated per-request, the ModelValidatorProvider appears to be long-lived and instantiated within the application startup. The WebAPI then caches the results from the

FluentValidation checking for duplicate entity in a sub-collection

空扰寡人 提交于 2019-12-10 09:28:52
问题 I have a MainEntity class and it has a collection of SubEntity . The following is the current validation: public class MainEntityValidator : AbstractValidator<MainEntity> { public MainEntityValidator() { RuleFor(x => x.SubEntities).SetCollectionValidator(new SubEntityValidator()); } public class SubEntityValidator : AbstractValidator<SubEntity> { public SubEntityValidator() { RuleFor(x => x.Field1).NotNull(); RuleFor(x => x.Field2).NotNull(); } } } How can I add a validation rule so that only

Using CascadeMode.StopOnFirstFailure on a validator level

妖精的绣舞 提交于 2019-12-10 02:39:44
问题 From the FluentValidation documentation I learned that I can abort validation by setting the cascade mode. RuleFor(x => x.Surname) .Cascade(CascadeMode.StopOnFirstFailure) .NotNull() .NotEqual("foo"); That way if the property Surname is null, the equality check won't be executed and a null pointer exception prevented. Further down in the documentation it is implied that this would also work not only within a rule but on a validator level as well. public class PersonValidator :

WebAPi - unify error messages format from ApiController and OAuthAuthorizationServerProvider

会有一股神秘感。 提交于 2019-12-10 00:40:15
问题 In my WebAPI project I'm using Owin.Security.OAuth to add JWT authentication. Inside GrantResourceOwnerCredentials of my OAuthProvider I'm setting errors using below line: context.SetError("invalid_grant", "Account locked."); this is returned to client as: { "error": "invalid_grant", "error_description": "Account locked." } after user gets authenticated and he tries to do "normal" request to one of my controllers he gets below response when model is invalid (using FluentValidation): {

FluentValidation Autofac ValidatorFactory

て烟熏妆下的殇ゞ 提交于 2019-12-09 11:18:53
问题 I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly IComponentContext context; public ValidatorFactory(IComponentContext context) { this.context = context; } public override IValidator CreateInstance(Type validatorType) { return context.Resolve(validatorType) as IValidator; } } How do I provide the context and register the