Skip to content

Custom FluentValidation translations for built-in validators. Translated errors used in API response. Very useful when implementing REST API.

License

Notifications You must be signed in to change notification settings

jaka-logar/LSolutions.Api.FluentValidation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LSolutions.Api.FluentValidation

Custom FluentValidation translations for most built-in validators. BaseValidationApiController implemented with common error responses. Return ErrorModel with ModelState fields errors and with array of custom errors.

Build status contributions welcome

Packed as two NuGet packages. Core project with translations and AspNetCore project:

  • LSolutions.Api.FluentValidation NuGet
  • LSolutions.Api.FluentValidation.AspNetCore NuGet

Usage

  1. Add code below in your Startup.cs
    public void ConfigureServices(IServiceCollection services)
	{
		services
			.AddControllers(configure =>
			{
				configure.Filters.Add(typeof(ValidateModelActionFilter));
			})

			// Fluent validation
			.AddFluentValidation(fv =>
			{
				// Register fluent validation as only validation library that executes
				fv.DisableDataAnnotationsValidation = true;

				fv.ImplicitlyValidateChildProperties = true;

				fv.RegisterValidatorsFromAssemblyContaining<IValidatorResult>();
				fv.RegisterValidatorsFromAssemblyContaining<Startup>();
			});
			
		
		// Fluent validation localization
		// https://fluentvalidation.net/localization
		ValidatorOptions.Global.LanguageManager = new ApiEnglishLanguageManager();
		ValidatorOptions.Global.LanguageManager.Culture = new CultureInfo("en");
}
  1. Extend ApiEnglishLanguageManager with translations for your custom validators.

About

Custom FluentValidation translations for built-in validators. Translated errors used in API response. Very useful when implementing REST API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages