I would like to have a way to specify for certain Javascript method which attributes are required, what pattern they should match, and how to respond if they are not.
This is because it results in a lot of repetitive code to check for required and optional parameters at the method level.
Take this example. Here I’d like to build a lightbox. If they send me a string, I’ll display a lightbox with just content. If they send me an options object, I look for a ‘title’ and ‘content’. Wouldn’t it be great to be able to specify this in some standardized way?
// Static method for generating a lightbox
// callerOptions = '' //if sent a string, the lightbox displays it with no title
// callerOptions = {
// content: '' // required popup contents. can be HTML or text.
// , title: '' // required title for the lightbox
// , subtitle: '' // optional subtitle for lightbox
// }
lightbox = function (callerOptions) {
if (!callerOptions) {
log.warn(_myName + ': calling me without a message to display or any options won\'t do anything');
return;
}
// If they send us a string, assume it's the popup contents
if (typeof(callerOptions) === 'string') {
this.options = {};
this.options.content = callerOptions;
// Otherwise assume they sent us a good options object
} else {
this.options = callerOptions;
}
_build();
_contentLoaded();
};
I’d love to be able to use some library I’ve never heard of to do something like this:
// Maybe this is what it looks like with a method signature enforcement library
lightbox = function (callerOptions) {
TheEnforcer(
, { valid: [
'string' // assumes that it is testing type against arguments by convention
, 'typeof([0].title) === "string" && typeof([0].content) === "string"'
]
}
});
// If they send us a string, assume it's the popup contents
if (typeof(callerOptions) === 'string') {
this.options = { 'content': callerOptions };
// Otherwise we know they sent us a good options object
} else {
this.options = callerOptions;
}
_build();
_contentLoaded();
};
Has anyone ever seen a Javascript library like this? Maybe built into one of the 1000 JS MV* Frameworks?
Edit:
Seems like this is usually taken care of by the MV* frameworks. Backbone.js has both validation and default values on it’s model’s properties. I think these could be used to meet, or nearly meet, the use case I present here.
I guess there are two parts to this, the philosophical/architectural and the implementation.
On the philosophical side, I think there is nothing simpler (for users of my API, not for me, the API developer) than clear expectations and error messages that describe what is and isn’t required for each method.
On the implementation side, which is what the question was originally about, I think I have to answer my own. Backbone.js’ Models, particularly the .validate portion, seem to fulfill this need pretty well. I have yet to use them but it’s the only thing I’ve found like it so far.
http://documentcloud.github.com/backbone/#Model-validate
EDIT:
Another solution could be compile time. Google Closure Compiler seems to handle this quite nicely: https://developers.google.com/closure/compiler/docs/js-for-compiler