index.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>apiCheck.js</title>
  6. </head>
  7. <body>
  8. This page is for manual testing of apiCheck.js. Just to give a little human aspect to developing the library :-)
  9. <script src="dist/api-check.js"></script>
  10. <script>
  11. // just... yeah...
  12. function require() {
  13. return apiCheck;
  14. }
  15. // From README
  16. var myApiCheck = require('api-check')({
  17. /* config options */
  18. output: {
  19. prefix: 'app/lib Name',
  20. suffix: 'Good luck!',
  21. docsBaseUrl: 'http://www.example.com/error-docs#'
  22. },
  23. verbose: false
  24. }, {
  25. /* custom checkers if you wanna */
  26. });
  27. // given we have a function like this:
  28. function foo(bar, foobar) {
  29. // we can define our api as the first argument to myApiCheck.warn
  30. myApiCheck.warn([myApiCheck.number, myApiCheck.arrayOf(myApiCheck.string)], arguments);
  31. // do stuff
  32. }
  33. // the function above can be called like so:
  34. foo(3, ['a','b','c']);
  35. // if it were called like so, a descriptive warning would be logged to the console
  36. foo('whatever', false);
  37. // here's something a little more complex (this is what's in the screenshot and [the demo](http://jsbin.com/hibocu/edit?js,console,output))
  38. var myCheck = require('api-check')({
  39. output: {
  40. prefix: 'myApp',
  41. suffix: 'see docs -->',
  42. docsBaseUrl: 'http://example.com/error-descriptions#'
  43. }
  44. });
  45. function doSomething(person, options, callback) {
  46. myCheck.warn([ // you can also do myCheck.throw to throw an exception
  47. myCheck.shape({
  48. name: myCheck.shape({
  49. first: myCheck.string,
  50. last: myCheck.string
  51. }),
  52. age: myCheck.number,
  53. isOld: myCheck.bool,
  54. walk: myCheck.func,
  55. ipAddress: function(val, name, location) {
  56. if (!/(\d{1,3}\.){3}\d{1,3}/.test(val)) {
  57. return myCheck.utils.getError(name, location, 'ipAddress');
  58. }
  59. },
  60. childrenNames: myCheck.arrayOf(myCheck.string).optional
  61. }),
  62. myCheck.any.optional,
  63. myCheck.func
  64. ], arguments, {
  65. prefix: 'doSomething',
  66. suffix: 'Good luck!',
  67. urlSuffix: 'dosomething-api-check-failure'
  68. });
  69. // do stuff
  70. }
  71. var person = {
  72. name: {
  73. first: 'Matt',
  74. last: 'Meese'
  75. },
  76. age: 27,
  77. isOld: false,
  78. ipAddress: '127.0.0.1',
  79. walk: function() {}
  80. };
  81. function callback() {}
  82. var options = 'whatever I want because it is an "any" type';
  83. console.log('Successful call');
  84. doSomething(person, options, callback);
  85. console.log('Successful call (without options)');
  86. doSomething(person, callback); // <-- options is optional
  87. console.log('Failed call (without person)');
  88. doSomething(callback); // <-- this would fail because person is not optional
  89. person.ipAddress = 'Invalid IP Address!!!';
  90. console.log('Failed call (invalid ip address)');
  91. doSomething(person, options, callback); // <-- this would fail because the ipAddress checker would fail
  92. // if you only wish to check the first argument to a function, you don't need to supply an array.
  93. var libCheck = apiCheck(); // you don't HAVE to pass anything if you don't want to.
  94. function bar(a) {
  95. var errorMessage = libCheck(apiCheck.string, arguments);
  96. if (!errorMessage) {
  97. // success
  98. } else if (typeof errorMessage === 'string') {
  99. // there was a problem and errorMessage would like to tell you about it
  100. }
  101. }
  102. bar('hello!'); // <-- success!
  103. // Further down in README
  104. var myCheck2 = require('api-check')({
  105. output: {prefix: 'myCheck2'}
  106. });
  107. function ipAddressChecker(val, name, location) {
  108. if (!/(\d{1,3}\.){3}\d{1,3}/.test(val)) {
  109. return apiCheck.utils.getError(name, location, ipAddressChecker.type);
  110. }
  111. }
  112. ipAddressChecker.type = 'ipAddressString';
  113. function foo2(string, ipAddress) {
  114. myCheck2.warn([
  115. myCheck2.string,
  116. ipAddressChecker
  117. ], arguments);
  118. }
  119. foo2('hello', 'not-an-ip-address');
  120. </script>
  121. </body>
  122. </html>