I am trying to manually validate a model (PageModel
) that has a property (Questions
) which is an IEnumerable of polymorphic types. I couldn't get this to work automatically so I am attempting to use the code below.
public IActionResult Submit([FromServices] IValidatorFactory factory, [FromForm] PageModel model)
{
foreach ( var question in model.Questions )
{
Type questionType = question.GetType();
var validator = factory.GetValidator(question.GetType());
var results = validator.Validate(ValidationContext.CreateWithOptions(question));
results.AddToModelState(ModelState, null);
}
However, IValidator.Validate()
does not take the model (question
) but instead needs a ValidationContext<T>
or an IValidationContext
, neither of which I can create without knowing the type of validator at compile time.
The example of manual validation in the docs shows a specific validator being created but I would like FluentValidation to find the correct one with the factory and then call it.
Does anyone know how to do this?