ilinsky/guard.js
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
------------------------------------ Guard.js - JavaScript API validation library ------------------------------------ About: Guard.js is a library that helps validating API calls in your project This is useful for authors of JavaScript libraries, not end-users Usage: // Your library code function myLibraryFunction(sName, fHandler, bDirection, oArguments) { // Validate API arguments Guard(arguments, [ ["name", String], // 'name' is required, must be string ["handler", Function], // 'handler' is required, must be function ["direction", Boolean, true, true], // 'direction' is optional, if passed must be boolean or null ["arguments", Guard.Arguments, true] // 'arguments' is optional, if passed must be JS arguments object ]); // Execute your library code here return "the output of myLibraryFunction"; }; // User code >>> myLibraryFunction() >>> [Exception... Missing 1st required argument "name" in "myLibraryFunction" function call] >>> myLibraryFunction("test", true) >>> [Exception... Incompatible type of 2nd argument "handler" in "myLibraryFunction" function call. Expected "Function", got "Boolean"] >>> myLibraryFunction("test", null) >>> [Exception... null is not allowed value of 2nd argument "handler" in "myLibraryFunction" function call] >>> myLibraryFunction("test", function(){}, undefined) >>> [Exception... Incompatible type of 3rd argument "direction" in "myLibraryFunction" function call. Expected "Boolean", got "undefined"] >>> myLibraryFunction("test", new Function) >>> the output of myLibraryFunction >>> (function(){return myLibraryFunction("test", new Function, true, arguments)})() >>> the output of myLibraryFunction Data types support: Primitive types - String - Number - Boolean - Object Complex Types - Array - Function - Date - RegExp Special Types - Guard.Arguments (to designate JavaScript function arguments type) User-defined types - any custom types supported Supplementary APIs: /*Boolean*/ Guard.instanceOf(/*Variant*/ value, /*Function*/ type) >>> Guard.instanceOf([], Array) >>> true >>> Guard.instanceOf("1", String) >>> true >>> Guard.instanceOf("2", Number) >>> false /*Function*/ Guard.typeOf(/*Variant*/ value) >>> Guard.typeOf(1) >>> Number() >>> Guard.typeOf(function(){}) >>> Function()
About
guard.js - Guards your JavaScript library or project by validating its APIs
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published