Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validators not applied to parameters with BindingSource = Form #7

Closed
steverb38 opened this issue Sep 7, 2023 · 7 comments · Fixed by #8
Closed

Validators not applied to parameters with BindingSource = Form #7

steverb38 opened this issue Sep 7, 2023 · 7 comments · Fixed by #8
Assignees
Milestone

Comments

@steverb38
Copy link

Hi,

Thanks for providing this package for auto FluentValidation!

My first attempt as using this was trying to apply a Validator to a Form parameter but I see the code specifically only applies validators to Body or Query parameters. Not being a web developer particularly, is there a specific reason for this restriction?

More a question in education (for me) perhaps?

Steve.

@mvdgun
Copy link
Member

mvdgun commented Sep 8, 2023

Hi @steverb38,

I don't think there is a specific reason for not having validation on [FromForm] parameters other than that I never needed it in my personal projects (from which this library sprouted). I'm currently thinking about introducing configuration options for the BindingSources. Could you provide me with some examples where you would need validation on [FromForm] parameters?

@steverb38
Copy link
Author

Hi,

My use-case was submitting a form for a file upload to the server, along with some associated parameters to control the behaviour of the API in processing the uploaded file. The API action method looks something like:

[HttpPost]
public async Task<ActionResult<MyResponse>> Post([FromForm] MyRequest request)
{
...
}

where MyRequest is like:

public class MyRequest
{
    public IFormFile File { get; set; }

    public string Param1 { get; set; }

    public bool Param2 { get; set; }

    ....
}

There were then some validations to perform on the Param1, Param2,... values to ensure together they made sense.

Steve.

@steverb38
Copy link
Author

OK - I updated a local copy of FluentValidationAutoValidationActionFilter.cs on line 55 to allow a BindingSource of Form as well as Body and Query.

image

That seemed to then work OK in my test scenario. No other testing done though!

@mvdgun
Copy link
Member

mvdgun commented Sep 13, 2023

@steverb38 that's great to hear! I actually have been playing with the idea to expose the binding sources via the options builder and via the data annotations attributes to make it all configurable. The default would be BindingSource.Body, BindingSource.Body and BindingSource.Query. The only issue I see with the defaults is if I should consider this a breaking change.

@mvdgun mvdgun linked a pull request Sep 14, 2023 that will close this issue
@mvdgun
Copy link
Member

mvdgun commented Sep 14, 2023

Opened a PR for it: https://github.com/SharpGrip/FluentValidation.AutoValidation/pull/8/files. Would appreciate it if you could take a look at it :).

@mvdgun mvdgun self-assigned this Sep 14, 2023
@mvdgun mvdgun added this to the v1.2 milestone Sep 14, 2023
@steverb38
Copy link
Author

Hi. Fantastic! I'll take a look later today. 👍

@mvdgun mvdgun closed this as completed in #8 Sep 22, 2023
@mvdgun
Copy link
Member

mvdgun commented Sep 22, 2023

Hi @steverb38, I just released v1.2.0 which includes the configurable binding sources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants