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

Add extension methods to the ModelStateDictionary type #55

Closed
MrDave1999 opened this issue Dec 16, 2023 · 0 comments · Fixed by #56
Closed

Add extension methods to the ModelStateDictionary type #55

MrDave1999 opened this issue Dec 16, 2023 · 0 comments · Fixed by #56
Labels
feature New feature or request

Comments

@MrDave1999
Copy link
Owner

MrDave1999 commented Dec 16, 2023

These extension methods will be added to the ModelStateDictionary type:

public static class SRModelStateDictionaryExtensions
{
    public static bool IsFailed(this ModelStateDictionary modelState);
    public static Result Invalid(this ModelStateDictionary modelState);
    public static Result Invalid(this ModelStateDictionary modelState, string message);
    public static BadRequestObjectResult BadRequest(this ModelStateDictionary modelState);
    public static BadRequestObjectResult BadRequest(this ModelStateDictionary modelState, string message);
}

SRModelStateDictionaryExtensions contains methods that convert an instance of ModelStateDictionary into an instance of type Result.
The SR "prefix" is so as not to cause name conflicts.

Usage

[Route("api/[controller]")]
public class PersonController : ControllerBase
{
    [HttpPost]
    public ActionResult<Result<CreatedId>> Create([FromBody]Person person)
    {
        if (ModelState.IsFailed())
            return ModelState.BadRequest();

        // Some code..
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant