1

I am trying to create an API using best practices with a simple validation by example.

public class ClientController
{
       [HttpGet]
       [Authorize] 
       public IHttpActionResult Clients([FromUri]string Initialdate,[FromUri] string finalDate)
       {
           return _clientRepository.GetClients();
       }
}


public class ClientRepository
{
        //some code that access data layer
        public IEnumerable<Client> GetClients(string initialDate,string finalDate)
        {
             //HERE IS WHAT IM LOOKING FOR
             //HERE I WANT TO VALIDATE THE DATE PARAMETER PROVIDED IN REQUEST AND RETURN 
             //VALIDATION LIST WITH ERROR DESCRIPTION IN JSON

             _daoClient.ExecuteProcedure(initialDate,finalDate);
        }
}


public class Client
{
    public int Id {get;set;}
    public string Name {get;set;}
}

So, how can i build this simple validation of parameters in request with action like a parameter filters?

5
  • I suggest not to do validation inside your repository. There are few ways you can validate check out this blog Commented Sep 17, 2019 at 15:21
  • Seen this? Commented Sep 17, 2019 at 15:26
  • You want to add attribute validation on class Client properties? Commented Sep 17, 2019 at 18:34
  • No, i want to validate query parameters from get method, and return in a list of Json the problems with them. Example: GetClient with invalid date, and return the error , thats why i can't use modelstate. because it is like an filter than propertie from a model. Commented Sep 17, 2019 at 19:41
  • @RenanDuarte please check my answer below, you can do like this. Commented Sep 18, 2019 at 7:58

1 Answer 1

2

You have to do validate explicitly like this then:

[HttpGet]
[Authorize]
public IHttpActionResult Clients([FromUri]string Initialdate, [FromUri] string finalDate)
{
         bool isInitialDate = DateTime.TryParse(Initialdate, out DateTime tempInitialdate);
         bool isFinalDate = DateTime.TryParse(finalDate, out DateTime tempfinalDate);

         List<string> _errorMessage = new List<string>();

         if (!isInitialDate)
              _errorMessage.Add("Initial date is invalid");

         if(!isFinalDate)
             _errorMessage.Add("Final date is invalid");

         if(isInitialDate && isFinalDate)
         {
                //your business logic
                return _clientRepository.GetClients();
         }

            return Json(new { success = false, error = _errorMessage });
}

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Nice awais, but i was thinking to abstract this validation from controller and transfer the resposability to another layer, and return erros with list of errors found or success with the information that was requested by the user.
Dear @RenanDuarte I got your point for that you need to create an interceptor class and configure in startup.cs`` e.g.: config.MessageHandlers.Insert(0, new ApiDelegatingHandler());``` in ApiDelegatingHandler class you need to inherit public class ApiDelegatingHandler : DelegatingHandler and write your custom validation in order to achieve the above requirement. I would highly recommend using the above logic to validate at controller level.
Firstly, thank you for contribuition. So, can you tell me the reason to maintain the parameter validation at the ontroller level ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.