Problem
I am using fluent validation for model validation and I want it to be done by ValidationFilter in azure function. I already did it in asp.net core app but I don't know how to do it in azure functions
Code
MyValidator
using FluentValidation;
namespace MyLocker.Models.Validations
{
public class PortfolioFolderVMValidator : AbstractValidator<PortfolioFolderVM>
{
public PortfolioFolderVMValidator()
{
RuleFor(reg => reg.Title).NotEmpty().WithName("Title").WithMessage("{PropertyName} is required");
RuleFor(reg => reg.UserId).NotEmpty().WithName("UserId").WithMessage("{PropertyName} is required");
}
}
}
Validate Model Action Filter
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Mvc.Filters;
using MyLocker.Models.Common;
namespace MyLocker.WebAPI.Attributes
{
public sealed class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
var errors = context.ModelState.Keys.SelectMany(key => context.ModelState[key].Errors.Select(error => new ValidationError(key, error.ErrorMessage))).ToList();
context.Result = new ContentActionResult<IList<ValidationError>>(HttpStatusCode.BadRequest, errors, "BAD REQUEST", null);
}
}
}
}
Startup
after that add it in startup.cs class like this
services.AddMvc(opt => opt.Filters.Add(typeof(ValidateModelAttribute)))
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<PortfolioFileModelValidator>())
I tried it in azure function but there are different classes and interface in it.