fluentvalidation

Adding DataAnnotation to class when using FluentValidation

时间秒杀一切 提交于 2019-12-01 08:03:06
问题 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

How can I prevent a VerificationException when running a test with attached debugger?

浪尽此生 提交于 2019-12-01 04:48:00
Whenever I run either of the following unit test with a debugger attached, I get a VerificationException inside FluentValidation code at this point (will post whole stacktrace later if necessary): at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression`1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) in ...\FluentValidation\Resources\LocalizedStringSource.cs:line 66 The tests are: using FluentValidation; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var c =

How can I prevent a VerificationException when running a test with attached debugger?

淺唱寂寞╮ 提交于 2019-12-01 02:23:56
问题 Whenever I run either of the following unit test with a debugger attached, I get a VerificationException inside FluentValidation code at this point (will post whole stacktrace later if necessary): at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression`1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) in ...\FluentValidation\Resources\LocalizedStringSource.cs:line 66 The tests are: using FluentValidation; using Microsoft.VisualStudio

Configure NancyFx with Fluent Validation

假装没事ソ 提交于 2019-11-30 13:08:41
Is there any configuration code that I have to add in the application Bootstrapper to enable FluentValidation in Nancy? Following the example from https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Demo.Validation I receive the following exception message when trying to use this.Validate on model: No model validator factory could be located . I'm using Nancy version 0.11.0.0 TheCodeJunkie Are you using one of the Bootstrapper packages (autofac, ninject, unity, windsor, structuremap)? If you are then you need to inherit from the bootstrapper type, override ConfigureApplicationContainer and

Conditional Validation using Fluent Validation

三世轮回 提交于 2019-11-30 11:12:18
问题 What I need is a way to conditionally validate fields depending on if other fields are filled in. Ex. I have a dropdown and a date field that are related. If none of the fields are set then the form should pass validation. However, if one of the two fields are set but the other isn't then the validation should fire, requiring the other field to be set. I have written custom validation classes but it seems that it is validates on single fields. Is there a way to set up the validation that I

Validate DateTime with FluentValidator

♀尐吖头ヾ 提交于 2019-11-30 08:30:09
This is my ViewModel class: public class CreatePersonModel { public string Name { get; set; } public DateTime DateBirth { get; set; } public string Email { get; set; } } CreatePerson.cshtml @model ViewModels.CreatePersonModel @{ ViewBag.Title = "Create Person"; } <h2>@ViewBag.Title</h2> @using (Html.BeginForm()) { <fieldset> <legend>RegisterModel</legend> @Html.EditorForModel() <p> <input type="submit" value="Create" /> </p> </fieldset> } CreatePersonValidator.cs public class CreatePersonValidator : AbstractValidator<CreatePersonModel> { public CreatePersonValidator() { RuleFor(p => p.Name)

How do you validate against each string in a list using Fluent Validation?

浪尽此生 提交于 2019-11-30 08:06:16
问题 I have an MVC3 view model defined as: [Validator(typeof(AccountsValidator))] public class AccountViewModel { public List<string> Accounts { get; set; } } With the validation defined using FluentValidation (v3.3.1.0) as: public class AccountsValidator : AbstractValidator<AccountViewModel> { public AccountsValidator() { RuleFor(x => x.Accounts).SetCollectionValidator(new AccountValidator()); //This won't work } } And the account validation would possibly be defined: public class

Fluent Validation with Swagger in Asp.net Core

末鹿安然 提交于 2019-11-30 05:10:48
I am currently using Fluent Validation instead of Data Annotations for my Web api and using swagger for API documentation. Fluent validation rules are not reflected in swagger model as i am unable to configure fluent validation rules with swagger schema filter. This Blog has a good explanation for using it with ASP.net MVC. but i am unable to configure it to use it in ASP.net Core. So far i have tried the following code but i am unable to get validator type. services.AddSwaggerGen(options => options.SchemaFilter<AddFluentValidationRules>()); public class AddFluentValidationRules :

How can I access the collection item being validated when using RuleForEach?

自闭症网瘾萝莉.ら 提交于 2019-11-30 03:05:12
I'm using FluentValidation to validate an object, and because this object has a collection member I'm trying to use RuleForEach . For example, suppose we have Customer and Orders , and we want to ensure that no customer order has a total value that exceeds the maximum allowed for that customer: this.RuleForEach(customer => customer.Orders) .Must((customer, orders) => orders.Max(order => order.TotalValue) <= customer.MaxOrderValue) So far, so good. However, I also need to record additional information about the context of the error (e.g. where in a data file the error was found). I've found

Conditional Validation using Fluent Validation

筅森魡賤 提交于 2019-11-30 02:35:19
What I need is a way to conditionally validate fields depending on if other fields are filled in. Ex. I have a dropdown and a date field that are related. If none of the fields are set then the form should pass validation. However, if one of the two fields are set but the other isn't then the validation should fire, requiring the other field to be set. I have written custom validation classes but it seems that it is validates on single fields. Is there a way to set up the validation that I need using the built in validators? If not, Is there a good way to connect two fields using a custom