fluentvalidation

Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest

亡梦爱人 提交于 2019-11-30 02:31:34
I'm trying to use Autofac to inject dependencies into FluentValidation in an MVC 4 app. I think I've got the strategy worked out, but I'm getting stuck with resolving my per-request ISomething from a singleton. Here's the scenario: I've got a validator that derives from FluentValidation's AbstractValidator. I've read that FluentValidation validators perform best as singletons, so my constructor expects a Func and stores that Factory for use later. When the validator is used, it should ask the stored factory for an IDataStore, get the instance created for that request and use it. That's the

FluentValidation: Check if one of two fields are empty

别等时光非礼了梦想. 提交于 2019-11-30 01:18:17
I have this model public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } I want to create a validation where either FirstName or LastName must be filled in by user. I installed FluentValidation and created a customvalidator class public class PersonValidator:AbstractValidator<Person> { public PersonValidator() { RuleFor((person=>person.FirstName)//don't know how to check if one is empty } } To check just one field I could just do RuleFor(person => person.FirstName).NotNull(); But how do I check if one of them is null. Also

Configure NancyFx with Fluent Validation

混江龙づ霸主 提交于 2019-11-29 18:17:38
问题 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 回答1: Are you using one of the Bootstrapper packages (autofac, ninject, unity, windsor, structuremap)? If you are

Validate DateTime with FluentValidator

[亡魂溺海] 提交于 2019-11-29 11:44:55
问题 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

FluentValidation client-side validation

会有一股神秘感。 提交于 2019-11-29 06:31:50
问题 I tried to use GreaterThen validator and it looks like it doesn't support client-side validation. Is there a list of FluentValidation validators which support client-side validation? 回答1: The list of validators supported on the client is on this page and are as follows: NotNull/NotEmpty (required) Matches (regex) InclusiveBetween (range) CreditCard Email EqualTo (cross-property equality comparison) Length 回答2: So far i know there is no list, you could create your own client side validator so

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

青春壹個敷衍的年華 提交于 2019-11-29 05:46:24
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 AccountValidator : AbstractValidator<string> { public OrderValidator() { RuleFor(x => x).NotNull(); //any other

Fluent Validation with Swagger in Asp.net Core

偶尔善良 提交于 2019-11-29 02:53:20
问题 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 =>

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

廉价感情. 提交于 2019-11-29 00:42:20
问题 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

Child Model Validation using Parent Model Values. Fluent Validation. MVC4

萝らか妹 提交于 2019-11-28 22:28:20
问题 Below is a simplified version of my problem. I can not flatten the model. There is a List of "children" that I need to validate a birthday. I can not seem to reference the date in the Parent class and was wondering how this is done in Fluent Validation? Model [Validator(typeof(ParentValidator))] public class Parent { public string Name { get; set; } public DateTime Birthdate { get; set; } public List<Child> Children { get; set; } } public class Child { public string ChildProperty{ get; set; }

FluentValidation - Validating a View Model that contains a list of an Object

删除回忆录丶 提交于 2019-11-28 22:26:42
问题 I am trying out FluentValidation on a project that contains complex view models and I read the documentation here but I don't see how to set up the rules to validate a list of objects declared in my view model. In my example below, the list in the view model contains 1 or more Guitar objects. Thanks View Model [FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))] public class CustomerViewModel { [Display(Name = "First Name")] public string FirstName { get; set; }