2. 1. What is wrong with my code? (bad library story) jQuery: > $(document).each() TypeError: b is undefined > $(document).click("alert(1)") TypeError: d is undefined > $(document).map(null) TypeError: a is null Dojo: > dojo.stopEvent() TypeError: evt is undefined > dojo.ready([new Function]) TypeError: mll[x] is not a function > dojo.map(null) TypeError: arr is null
3. 1. What is wrong with my code? (good library story) An other library (Ample SDK): > ample.documentElement.removeChild() Missing required 1st argument "node" in "removeChild" function call > ample.querySelector("xul|textbox[type=number]", 11); Incompatible type of 2nd argument "NSResolver" in "querySelector" function call. Expecting "Function" > ample.importNode(null) null is not allowed value of 1st argument "node" in "importNode" function call
4. 2. Why is it important to validate library API call?
5. 2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently
6. 2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently 2) Prevents library from internal failure caused by improper call
9. 4. Guard.js — API validation library Guard.js is a “broker library” that helps you validating API calls to your “public library». 4.1) Using Guard.js 4.2) Guard exceptions in try/catch statements 4.3) Data types support 4.4) Validation execution overhead
10. 4.1 Using Guard.js // Your «public library» code myObject.myFunction = function(sName, fHandler, bDirection, oArguments) { // Validate API function call Guard(arguments, [ // 'name' is required, must be string ["name", String], // 'handler' is required, must be function ["handler", Function], // 'direction' is optional, if passed must be boolean or null ["direction", Boolean, true, true], // 'arguments' is optional, if passed must be JS arguments object ["arguments", Guard.Arguments, true] ]); // Execute your library function here return "the output of myLibraryFunction"; };
12. 4.3 Data types support - Primitive Types - String - Number - Boolean - Object - «Object» Types - Array - Function - Date - RegExp - Special Types - Guard.Arguments - Custom Types - Any constructor function reference