Search code examples
c#asp.net-coreasp.net-web-apidata-annotationsfluentvalidation

FluentValidation for simple/scalar type string parameter


I'm using FluentValidation for complex type validation on POST actions. Is it possible to use the same framework to validate simple/scalar types like DataAnnotations does with Attributes on parameters for GET actions?

    [HttpGet("{code}")]
    public async Task<ActionResult> GetByCode(
        [StringLength(maximumLength: 8)]         //DataAnnotation <-------
        [FromRoute]
        string code
        )
    {
    .........

I would like to use just one validation framework instead of use DataAnnotations too.


Solution

  • Specifically with regard to FluentValidation, the library isn't designed for this. Jeremy (the author) specifically addressed this question in a GitHub issue that asked something similar to what you're asking. He basically said FV isn't designed for this (https://github.com/FluentValidation/FluentValidation/issues/337). You're going to need to stick to attributes for simple validation of simple parameters.