Skip to main content


Making FluentValidation compatible with Swagger including Enum or fixed List support

FluentValidation is not directly compatible with Swagger API to validate models. But they do provide an interface through which we can compose Swagger validation manually. That means we look under FluentValidation validators and compose Swagger validator properties to make it compatible. More of all mapping by reading information from FluentValidation and setting it to Swagger Model Schema.
These can be done on any custom validation from FluentValidation too just that proper schema property has to be available from Swagger.
Custom validation from Enum/List values on FluentValidation using FluentValidation.Validators; using System.Collections.Generic; using System.Linq; using static System.String; /// <summary> /// Validator as per list of items. /// </summary> /// <seealso cref="PropertyValidator" /> public class FixedListValidator : PropertyValidator { /// <summary> /// Gets the valid items /// </sum…
Recent posts

Trim text in MVC Core through Model Binder

Trimming text can be done on client side codes, but I believe it is most suitable on MVC Model Binder since it would be at one place on infrastructure level which would be free from any manual intervention of developer. This would allow every post request to be processed and converted to a trimmed string.

Let us start by creating Model binder

using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System.Threading.Tasks; public class TrimmingModelBinder : IModelBinder { private readonly IModelBinder FallbackBinder; public TrimmingModelBinder(IModelBinder fallbackBinder) { FallbackBinder = fallbackBinder ?? throw new ArgumentNullException(nameof(fallbackBinder)); } public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); } var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingC…

Custom authorization based on dotnet core policy with Attribute filter

Around 2.5 years back I had written about custom authorization on MVC  Custom authorization on class, action/function, code, area level under Asp.Net MVC application, there are few approaches which are changed in Core version for authorization. Like Authorization filter approach is discouraged since it cannot be unit tested. I believe this is right step but also global or basic authentication could still be driven by Attribute due to enhancing simplicity on codes by focusing on the primary objective rather than writing authorization check everywhere.

The whole approach and usage remain same from the original Post, in this, we would be just looking into making it compatible with dotnet Core MVC. You would need to go through earlier Post to understand the approach that was taken for authorization of a user.

Also, can go through official post: to understand new approach.

More of all we need to create Requiremen…

Voice control Sony Bravia Television through Alexa

This is my second useful thing done through Alexa after simple implementation of switching on/off light. This is not just applicable to Sony Bravia TVs but any device which can be controlled through HTTP/JSON request or via any other protocol.

Hardware prerequisites for making whole thing work are as follows:
1. Sony Bravia Android TV or other devices which can accept input through HTTP or different protocol.
2. Raspberry Pi to keep running program/service.
3. Alexa device.

Software prerequisites:
1. Alexa Skill:
2. Lambda:
3. AWS IoT:

How the whole process would work? Alexa would accept voice commands and converts it to intend to make a request to Lambda function. Lambda function would use converted user-friendly commands to MQTT request on AWS IoT service which would be listened through MQTT client running…

Centralized model validation both for MVC/WebApi and SPA client-side validation using FluentValidation

Validation is one of the crucial parts of any application. It has to validate on both client side and server side requests.
What are target features or implementation from this article?Model validation for any given model.Centralized/One code for validation on both server-side and client-side.Automatic validation of model without writing any extra codes on/under actions for validation. NO EXTRA/ANY codes on client-side to validate any form.Compatible with SPA.Can be compatible with any client-side validation framework/library. Like Angular Reactive form validation or any jquery validation libraries. Tools used in the implementation?FluentValidation: I feel DataAnnotation validation are excellent and simple to use, but in case of complex validation or writing any custom validations are always tricker and need to write a lot of codes to achieve whereas FluentValidations are simple even in case of complex validation. Generally, we need to validate incoming input against database values,…