I got something here that bogs my mind a bit.
Let's say I write me this API (in TS), check out some of these properties:
export class MyAPI{
propertyThatShouldContainSuffix:Array<string>; // like .jpg or .mp3
somethingElses:Array<SomethingElse>; //instances of some class
enumProperty:SomeEnum; // enum SomeEnum{a,b,c,d}
constructor(object){
/*
this object is input by the API consumer,
and its properties will be assigned to the new fields of
the new instance
*/
}
}
Valid usage example:
var myApi = new MyAPI({
propertyThatShouldContainSuffix : ["img.jpg","video.mp4" ...],
somethingElses : [new SomethingElse(/*yada yada*/),new SomethingElse(/* whateverrr*/) ...],
enumProperty:2
});
Input that may cause problems:
var myApi = new MyAPI({
propertyThatShouldContainSuffix : ["img","video",5 ...],
somethingElses : [new SomethingElse(/*yada yada*/),new SomethingTotallyElse(/* whateverrr*/) ...],
enumProperty:6
});
As you can see, the first property is an array of strings that need to have a suffix, like an image, that should be .jpg or .png or whatever. There is an array of objects that should contain some fields, and finally an enum field, let's say that it ranges from 0 to 3.
Now, it all works fine and stuff when you input the expected values into it (e.g all strings in first array has the right suffix and so on).
But then I thought that I should handle bad input, like a user that will send all his image names without any suffix, or will give me a "9" as input for the enum, send objects instead of arrays, and so on.
BUT! and here's the problem: how far should I go with this? should I check that every property is correct(e.g what is supposed to be an array is really an array, that all "supposed to be suffixed" are suffixed, that all "somethingelses" contain all correct fields?
Because if I do, this is a whole mess of overhead on every creation of an instance of MyAPI object.
Or should I only do something real basic like check if he didn't misspell some field in the object(therefore exposing helpless users to the perils of "but why isn't this working? stupid stupid API!") ?
Or anything inbetween?