An isomorphic, runtime, type checking utility that can be used on the server or in the client. Check was extracted from the Meteor framework, and has had some features added and been given full test coverage. It is used in the reactive framework milojs It allows to both document and to check parameter types in your function making code both readable and stable.
var check = milo.check
, Match = check.Match;
function My(name, obj, cb) {
// if any of checks fail an error will be thrown
check(name, String);
check(obj, Match.ObjectIncluding({ options: Object }));
check(cb, Function);
// ... your code
}
See Meteor docs to see how it works
All patterns and functions described in Meteor docs work.
Unlike in Meteor, Object pattern matches instance of any class, not only plain object.
In addition to patterns described in Meteor docs the following patterns are implemented
Match.ObjectHash(pattern)
Matches an object where all properties match a given pattern
Match.Subclass(constructor [, matchThisClassToo])
Matches a class that is a subclass of a given class. If the second parameter is true, it will also match the class itself.
Without this pattern to check if MySubclass is a subclass of MyClass you would have to use
check(MySubclass, Match.Where(function() {
return MySubclass.prototype instanceof MyClass;
});
Things we explicitly do NOT support:
- heterogenous arrays