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