0

I have a web API that takes JSON. This JSON contains 3 parameters. As part of the internal processing this API will call another web API (B2B). only 1 parameters will be passed to the second API (madatory). My question is that should my API validate the parameter that will be passed to the second API or the second API should validate them ?

My WebAPI (parm1,parm2,parm3){
''''''
string result= anotherApi.dosomething(parm3)

'''''

}

In the above example should I validate parm3 (which is mandatory by the second API) so that I won't call the anotherApi or should I just let the anotherApi return an error to my api that I will return to the caller?

  • The second API should validate them, it's like method. Method should validate all input params before processing – cuongle Mar 13 '17 at 15:33
  • @CuongLe what about mandatory field ? I mean the parm3 used on the second API is mandatory. If I check it on the first API I will avoid to call the second API. for validation I mean checking if a mandatory parameter has been provided or not – Rock Ninja Mar 13 '17 at 15:50
  • As a thumb rule, I would do all the cosmetic validations (required, data format etc) as early as possible, and then leave the business rules validations on the components handling the specific business component – Jinish Mar 13 '17 at 16:02
  • It's similar to javascript client-side validation vs server-side validation. Sure, the 2nd API should (and no doubt will) validate parameters, but if you know it's mandatory, then check before calling and provide a sensible error. If you have control over both, then you can leave off the first check incase the 2nd api changes in future and then your 2nd api can provide a meaningful error message and you don't need to repeat the error message. – freedomn-m Mar 13 '17 at 17:00

1 Answers1

0

It's good to validate model with low cost, so in this case it's better to validate your model in first level, and also it's proper to get your model class instead param1,param2,param3

public class YourModel
{
    public int param1 { get; set; }
    public int param2 { get; set; }
    public int param3 { get; set; }
}

and also you can validate each parameters with some techniques such as Validation Attribute or some third party such as Fluent Validation. check out these links

Model Validation in Web API Custom Validation in ASP.NET Web API with Fluent Validation