Validating inputs
Input validation is the act of verifying that any inputs to your code, such as parameters or current property values, are correct before performing the requested work. We validate inputs to public methods to detect potential issues early on.
To illustrate the importance of this, let’s look at a method that doesn’t validate its inputs:
public FlightInfo? GetFlight(string id, string apiKey) { RestRequest request = new($"/flights/{id.ToLower()}"); request.AddHeader("x-api-key", apiKey); LogApiCall(request.Resource); return _client.Get<FlightInfo?>(request); }
The GetFlight
method takes in an id
parameter indicating a flight number, such as “CSA1234,” whereas the apiKey
parameter represents a token that must be supplied to interact with the API and get a response. Think of the token as something like a digital keycard that Cloudy Skies issues to interested organizations...