fluentvalidation

Using FluentValidation's WithMessage method with a list of named parameters

二次信任 提交于 2019-12-03 10:10:27
I am using FluentValidation and I want to format a message with some of the object's properties value. The problem is I have very little experience with expressions and delegates in C#. FluentValidation already provides a way to do this with format arguments. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {1} is not valid for Id {0}", x => x.Id, x => x.Name); I would like to do something like this to avoid having to change the message string if I change the order of the parameters. RuleFor(x => x.Name).NotEmpty() .WithMessage("The name {Name} is not valid for Id {Id}", x => new { Id =

FluentValidation rule for multiple properties

与世无争的帅哥 提交于 2019-12-03 09:24:01
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 method to achieve this? bpruitt-goddard There is a Must overload that also provides you with the FooArgs

Stop Fluent Validation on first failure

落花浮王杯 提交于 2019-12-03 08:53:53
问题 i'm defining a validation for my Request objects. I would like the validator to stop on the very first failure, not only the one on the same chain. In the example below, if my TechnicalHeader object is null, i get a NullReference exception when the validation reaches the rule for TechnicalHeader.MCUserid . In poor words, i would like to do a conditional validation over the last three rules in the code below, according to the result of the first rule using System; using ServiceStack

Selective validation of child properties - Fluent Validation in MVC

℡╲_俬逩灬. 提交于 2019-12-03 08:47:47
I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of my validators (and use dependency injection to create the validators). I created the following Models: public class Parent { public string Name { get; set; } public Child Child1 { get; set; } public Child Child2 { get; set; } } public class Child { public string ChildProperty { get; set; } } With the following validators: public class ParentValidator : AbstractValidator<Parent> { public ParentValidator() { RuleFor(model => model.Name).NotEmpty(); RuleFor(model => model.Child1)

ASP.net MVC Validation Hook

末鹿安然 提交于 2019-12-03 08:42:54
问题 I have the following view in ASP.net MVC 3: @model Models.CreateProjectViewModel <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> @using( Html.BeginForm() ) { @Html.TextBoxFor(m => m.ProjectName) @Html.ValidationMessageFor(m => m.ProjectName) <p> <input type="submit" value="Save" /> </p> } I am using unobtrusive javascript with jQuery and

What is the correct way to register FluentValidation with SimpleInjector?

守給你的承諾、 提交于 2019-12-03 07:12:46
I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory . However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory : The composition root : public class SimpleDependencyInjector : IServiceProvider { public readonly Container Container; public SimpleDependencyInjector() { Container = Bootstrap(); } internal Container Bootstrap() { var container = new Container(); container.Register< // ...register all non-fluent-validator

Add validation to a MediatR behavior pipeline?

假如想象 提交于 2019-12-03 06:57:14
问题 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();

Dependency-Injected Validation in Web API

梦想与她 提交于 2019-12-03 00:48:53
In MVC, I can create a Model Validator which can take Dependencies. I normally use FluentValidation for this. This allows me to, for example, check on account registration that an e-mail address hasn't been used (NB: This is a simplified example!): public class RegisterModelValidator : AbstractValidator<RegisterModel> { private readonly MyContext _context; public RegisterModelValidator(MyContext context) { _context = context; } public override ValidationResult Validate(ValidationContext<RegisterModel> context) { var result = base.Validate(context); if (context.Accounts.Any(acc => acc.Email ==

Stop Fluent Validation on first failure

蓝咒 提交于 2019-12-02 22:52:14
i'm defining a validation for my Request objects. I would like the validator to stop on the very first failure, not only the one on the same chain. In the example below, if my TechnicalHeader object is null, i get a NullReference exception when the validation reaches the rule for TechnicalHeader.MCUserid . In poor words, i would like to do a conditional validation over the last three rules in the code below, according to the result of the first rule using System; using ServiceStack.FluentValidation; using MyProj.Services.Models; namespace MyProj.Services.BaseService.Validators { public class

DateTime client-side validation fails due to formatting

拜拜、爱过 提交于 2019-12-02 22:08:26
问题 I'm using FluentValidation in an MVC5 project without any issues except for dates. My goal is to have GreaterThanOrEqualTo client-side validation working for a few date fields. I know that the current documentation doesn't list this method as supported client-side, but it renders the correct jQuery Validation data attributes. In my model, I have a property: [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] [Display(Name = "Credit