Questions tagged [fluentvalidation]

Fluent Validation is an ASP.NET MVC integrated validation framework that allows the developer to set validation rules using expressions. It is testable and completely compatible with the inversion of control (dependency injection) pattern and TDD (test-driven development) technique.

Fluent Validation is an open-source, ASP.NET MVC / ASP.NET Core integrated validation framework that allows the developer to set validation rules using expressions. It is testable and completely compatible with the Inversion-of-Control (dependency injection) pattern and TDD (test-driven development) technique.

It is one of a number of validation options, including Data Annotations.

Visit the official website to learn more.

Source code can be found here.

1427 questions
35
votes
1 answer

FluentValidation unique name validation using database

I have Category model that has Name field, and every category name must be unique. I have made validation and it works when I try to create new Category but I have problem when trying to edit it. For now it's just checking if the name exists and of…
Stan
  • 25,744
  • 53
  • 164
  • 242
31
votes
5 answers

Validate DateTime with FluentValidator

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 @{ …
ridermansb
  • 10,779
  • 24
  • 115
  • 226
31
votes
3 answers

Is it possible to combine [FromRoute] and [FromBody] in ASP.NET Core?

I have an action on API controller like this: [HttpPost] public async Task StartDeployment( [FromQuery]Guid deploymentId, [FromRoute]RequestInfo requestInfo, [FromBody]DeploymenRequest deploymentRequest) { } which is…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
31
votes
2 answers

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

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…
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
30
votes
6 answers

How to hook FluentValidator to a Web API?

I'm trying to hook Fluent Validation to my MVC WEB Api project, and it doesn't wanna work. When I use MyController : Controller -> works fine (ModelState.IsValid returns False) but when I use MyController :ApiController ... nothing. Does anyone have…
Marty
  • 3,485
  • 8
  • 38
  • 69
29
votes
2 answers

FluentValidation validate Enum value

I have the following model: public class ViewDataItem { public string viewName { get; set; } public UpdateIndicator updateIndicator { get; set; } } With the following enum: public enum UpdateIndicator { Original, Update, …
JaggenSWE
  • 1,950
  • 2
  • 24
  • 41
28
votes
2 answers

How to mock a method with an out parameter?

I am using a library that uses out parameters in a function and I need to test my code using that function. So, attempting to have mocks come to my rescue here, via Moq which I've been using in the rest of the project. Question I know there's a wall…
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
27
votes
6 answers

Use custom validation responses with fluent validation

Hello I am trying to get custom validation response for my webApi using .NET Core. Here I want to have response model like [{ ErrorCode: ErrorField: ErrorMsg: }] I have a validator class and currently we just check ModalState.IsValid for…
RajGan
  • 801
  • 3
  • 13
  • 28
27
votes
5 answers

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

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…
Jason
  • 4,557
  • 5
  • 31
  • 40
25
votes
2 answers

Fluent validation custom validation rules

I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set; } public string Email { get; set; } public string Password { get; set; } public string ListOfCategoriess { get; set;…
David Levin
  • 6,573
  • 5
  • 48
  • 80
25
votes
1 answer

Fluent Validations. Inherit validation classes

I used Fluent Validator. But sometimes I need create a hierarchy of rules. For example: [Validator(typeof(UserValidation))] public class UserViewModel { public string FirstName; public string LastName; } public class UserValidation :…
Ivan Korytin
  • 1,832
  • 1
  • 27
  • 41
25
votes
3 answers

Should I abstract the validation framework from Domain layer?

I am using FluentValidation to validate my service operations. My code looks like: using FluentValidation; IUserService { void Add(User user); } UserService : IUserService { public void Add(User user) { new…
24
votes
2 answers

WebAPi - unify error messages format from ApiController and OAuthAuthorizationServerProvider

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…
Misiu
  • 4,738
  • 21
  • 94
  • 198
23
votes
3 answers

Fluent Validation with Swagger in Asp.net Core

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…
Mujahid Daud Khan
  • 1,983
  • 1
  • 14
  • 23
21
votes
6 answers

FluentValidation message for nested properties

I have a class with a complex property: public class A { public B Prop { get; set; } } public class B { public int Id { get; set; } } I've added a validator: public class AValidator : AbstractValidator { public AValidator() { …
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
1
2
3
95 96