Gathering detailed insights and metrics for api-check
Gathering detailed insights and metrics for api-check
Gathering detailed insights and metrics for api-check
Gathering detailed insights and metrics for api-check
caniuse-api
request the caniuse data to check browsers compatibilities
npm-check
Check for outdated, incorrect, and unused dependencies.
superstruct
A simple and composable way to validate data in JavaScript (and TypeScript).
@octokit/oauth-methods
Set of stateless request methods to create, check, reset, refresh, and delete user access tokens for OAuth and GitHub Apps
npm install api-check
Typescript
Module System
Node Version
NPM Version
JavaScript (96.4%)
HTML (3.6%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
188 Stars
162 Commits
21 Forks
6 Watchers
3 Branches
8 Contributors
Updated on Feb 08, 2024
Latest Version
7.5.5
Package Id
api-check@7.5.5
Size
73.17 kB
NPM Version
2.14.8
Node Version
3.3.1
Published on
Oct 26, 2015
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
35
If that build is ever red or the coverage is ever less than 100% then I want you to flame me on twitter (@kentcdodds) and be sure to mention how disappointed @josepheames would be in me
It's like ReactJS propTypes
without React. Actually,
it's very heavily inspired by this concept. It's purpose is for normal JavaScript functions rather than just React
Components.
$ npm i -S api-check
or $bower i -S api-check
api-check utilizes UMD, so you can:
var apiCheck = require('api-check')(/* your custom options, checkers*/);
Also available as an AMD module or as apiCheck
on global
Note, there are a bunch of tests. Those should be instructive as well.
1var myApiCheck = require('api-check')({ 2 /* config options */ 3 output: { 4 prefix: 'app/lib Name', 5 suffix: 'Good luck!', 6 docsBaseUrl: 'http://www.example.com/error-docs#' 7 }, 8 verbose: false 9}, { 10 /* custom checkers if you wanna */ 11}); 12 13// given we have a function like this: 14function foo(bar, foobar) { 15 // we can define our api as the first argument to myApiCheck.warn 16 myApiCheck.warn([myApiCheck.number, myApiCheck.arrayOf(myApiCheck.string)], arguments); 17 // do stuff 18} 19// the function above can be called like so: 20foo(3, ['a','b','c']); 21 22// if it were called like so, a descriptive warning would be logged to the console 23foo('whatever', false); 24 25 26// 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)) 27var myCheck = require('api-check')({ 28 output: { 29 prefix: 'myApp', 30 suffix: 'see docs -->', 31 docsBaseUrl: 'http://example.com/error-descriptions#' 32 } 33}); 34function doSomething(person, options, callback) { 35 myCheck.warn([ // you can also do myCheck.throw to throw an exception 36 myCheck.shape({ 37 name: myCheck.shape({ 38 first: myCheck.string, 39 last: myCheck.string 40 }), 41 age: myCheck.number, 42 isOld: myCheck.bool, 43 walk: myCheck.func, 44 ipAddress: function(val, name, location) { 45 if (!/(\d{1,3}\.){3}\d{1,3}/.test(val)) { 46 return myCheck.utils.getError(name, location, 'ipAddress'); 47 } 48 }, 49 childrenNames: myCheck.arrayOf(myCheck.string).optional 50 }), 51 myCheck.any.optional, 52 myCheck.func 53 ], arguments, { 54 prefix: 'doSomething', 55 suffix: 'Good luck!', 56 urlSuffix: 'dosomething-api-check-failure' 57 }); 58 59 // do stuff 60} 61 62var person = { 63 name: { 64 first: 'Matt', 65 last: 'Meese' 66 }, 67 age: 27, 68 isOld: false, 69 ipAddress: '127.0.0.1', 70 walk: function() {} 71}; 72function callback() {} 73var options = 'whatever I want because it is an "any" type'; 74 75console.log('Successful call'); 76doSomething(person, options, callback); 77 78console.log('Successful call (without options)'); 79doSomething(person, callback); // <-- options is optional 80 81console.log('Failed call (without person)'); 82doSomething(callback); // <-- this would fail because person is not optional 83 84person.ipAddress = 'Invalid IP Address!!!'; 85 86console.log('Failed call (invalid ip address)'); 87doSomething(person, options, callback); // <-- this would fail because the ipAddress checker would fail 88 89// if you only wish to check the first argument to a function, you don't need to supply an array. 90 91var libCheck = apiCheck(); // you don't HAVE to pass anything if you don't want to. 92function bar(a) { 93 var errorMessage = libCheck(apiCheck.string, arguments); 94 if (!errorMessage) { 95 // success 96 } else if (typeof errorMessage === 'string') { 97 // there was a problem and errorMessage would like to tell you about it 98 } 99} 100bar('hello!'); // <-- success!
Differences in Supported Types noted below with a *
.optional
element
and node
typesobject
fails on null. Use object.nullOk
if you don't want thatThis project was totally written from scratch, but it (should) support the same api as React's propTypes
(with the
noted difference above). If you notice something that functions differently, please file an issue.
These functions do the same thing, with minor differences. In both the warn
and throw
case, a message is generated
based on the arguments that the function was received and the api that was defined to describe what was wrong with the
invocation.
In all cases, an object is returned with the following properties:
This is an array of objects representing the types of the arguments passed.
This is an object representing the types of the api. It's a whole language of its own that you'll hopefully get after looking at it for a while.
Will be false when the check passes, and true when it fails
Will be true when the check passes, and false when it fails
If the check failed, this will be a useful message for display to the user. If it passed, this will be an empty string
Also note that if you only have one argument, then the first argument to the apiCheck
function can simply be the
checker function. For example:
1apiCheck(apiCheck.bool, arguments);
The second argument can either be an arguments-like object or an array.
1apiCheck.array([]); // <-- pass 2apiCheck.array(23); // <-- fail
1apiCheck.bool(false); // <-- pass 2apiCheck.bool('me bool too?'); // <-- fail
1apiCheck.func(function() {}); // <-- pass 2apiCheck.func(new RegExp()); // <-- fail
Not available in React's propTypes
1var checker = apiCheck.func.withProperties({ 2 type: apiCheck.oneOfType([apiCheck.object, apiCheck.string]), 3 help: apiCheck.string.optional 4}); 5function winning(){} 6winning.type = 'awesomeness'; 7checker(winning); // <--pass 8 9function losing(){} 10checker(losing); // <-- fail
1apiCheck.number(423.32); // <-- pass 2apiCheck.number({}); // <-- fail
null
fails, use object.nullOk
to allow null to pass
1apiCheck.object({}); // <-- pass 2apiCheck.object([]); // <-- fail 3apiCheck.object(null); // <-- fail
Not available in React's propTypes
1apiCheck.object.nullOk({}); // <-- pass 2apiCheck.object.nullOk([]); // <--- false 3apiCheck.object.nullOk(null); // <-- pass
1apiCheck.string('I am a string!'); // <-- pass 2apiCheck.string([]); // <-- fail
1apiCheck.range(0, 10)(4); // <-- pass 2apiCheck.rang(-100, 100)(500); // <-- fail
1apiCheck.greaterThan(100)(200); // <-- pass 2apiCheck.greaterThan(-10)(-20); // <-- fail 3apiCheck.greaterThan(50)('Frogs!'); // <-- fail
1apiCheck.lessThan(100)(50); // <-- pass 2apiCheck.lessThan(-10)(0); // <-- fail 3apiCheck.lessThan(50)('Frogs!'); // <-- fail
1apiCheck.instanceOf(RegExp)(new RegExp); // <-- pass 2apiCheck.instanceOf(Date)('wanna go on a date?'); // <-- fail
1apiCheck.oneOf(['Treek', ' Wicket Wystri Warrick'])('Treek'); // <-- pass 2apiCheck.oneOf(['Chewbacca', 'Snoova'])('Snoova'); // <-- fail
1apiCheck.oneOfType([apiCheck.string, apiCheck.object])({}); // <-- pass 2apiCheck.oneOfType([apiCheck.array, apiCheck.bool])('Kess'); // <-- fail
1apiCheck.arrayOf(apiCheck.string)(['Huraga', 'Japar', 'Kahless']); // <-- pass 2apiCheck.arrayOf( 3 apiCheck.arrayOf( 4 apiCheck.arrayOf( 5 apiCheck.number 6 ) 7 ) 8)([[[1,2,3], [4,5,6], [7,8,9]], [[1,2,3], [4,5,6], [7,8,9]]]); // <-- pass (for realz) 9apiCheck.arrayOf(apiCheck.bool)(['a', 'b', 'c']); // <-- fail
Not available in React's propTypes
Convenience checker that combines oneOfType
with arrayOf
and whatever you specify. So you could take this:
1apiCheck.oneOfType([ 2 apiCheck.string, apiCheck.arrayOf(apiCheck.string) 3]);
with
1apiCheck.typeOrArrayOf(apiCheck.string);
which is a common enough use case to justify the checker.
1apiCheck.typeOrArrayOf(apiCheck.string)('string'); // <-- pass 2apiCheck.typeOrArrayOf(apiCheck.string)(['array', 'of strings']); // <-- pass 3apiCheck.typeOrArrayOf(apiCheck.bool)(['array', false]); // <-- fail 4apiCheck.typeOrArrayOf(apiCheck.object)(32); // <-- fail
1apiCheck.objectOf(apiCheck.arrayOf(apiCheck.bool))({a: [true, false], b: [false, true]}); // <-- pass 2apiCheck.objectOf(apiCheck.number)({a: 'not a number?', b: 'yeah, me neither (◞‸◟;)'}); // <-- fail
Note: React propTypes
does support shape
, however it does not support the strict
option
If you add .strict
to the shape
, then it will enforce that the given object does not have any extra properties
outside those specified in the shape
. See below for an example...
1apiCheck.shape({ 2 name: checkers.shape({ 3 first: checkers.string, 4 last: checkers.string 5 }), 6 age: checkers.number, 7 isOld: checkers.bool, 8 walk: checkers.func, 9 childrenNames: checkers.arrayOf(checkers.string) 10})({ 11 name: { 12 first: 'Matt', 13 last: 'Meese' 14 }, 15 age: 27, 16 isOld: false, 17 walk: function() {}, 18 childrenNames: [] 19}); // <-- pass 20apiCheck.shape({ 21 mint: checkers.bool, 22 chocolate: checkers.bool 23})({mint: true}); // <-- fail
Example of strict
1var strictShape = apiCheck.shape({ 2 cookies: apiCheck.bool, 3 milk: apiCheck.bool, 4 popcorn: apiCheck.bool.optional 5}).strict; // <-- that! 6 7strictShape({ 8 cookies: true, 9 milk: true, 10 popcorn: true, 11 candy: true 12}); // <-- fail because the extra `candy` property 13 14strictShape({ 15 cookies: true, 16 milk: true 17}); // <-- pass because it has no extra properties and `popcorn` is optional
Note, you can also append .optional
to the .strict
(as in: apiCheck.shape({}).strict.optional
)
Not available in React's propTypes
This can only be used in combination with shape
1apiCheck.shape({ 2 cookies: apiCheck.shape.onlyIf(['mint', 'chips'], apiCheck.bool) 3})({cookies: true, mint: true, chips: true}); // <-- pass 4 5apiCheck.shape({ 6 cookies: apiCheck.shape.onlyIf(['mint', 'chips'], apiCheck.bool) 7})({chips: true}); // <-- pass (cookies not specified) 8 9apiCheck.shape({ 10 cookies: apiCheck.shape.onlyIf('mint', apiCheck.bool) 11})({cookies: true}); // <-- fail
Not available in React's propTypes
This can only be used in combination with shape
1apiCheck.shape({ 2 cookies: apiCheck.shape.ifNot('mint', apiCheck.bool) 3})({cookies: true}); // <-- pass 4 5apiCheck.shape({ 6 cookies: apiCheck.shape.ifNot(['mint', 'chips'], apiCheck.bool) 7})({cookies: true, chips: true}); // <-- fail
Not available in React's propTypes
This can only be used in combination with shape
1checker = checkers.shape({ 2 foobar: checkers.shape.requiredIfNot(['foobaz', 'baz'], checkers.bool), 3 foobaz: checkers.object.optional, 4 baz: checkers.string.optional, 5 foo: checkers.string.optional 6}); 7checker({ 8 foo: [1, 2], 9 foobar: true 10}); // <-- passes 11 12checker({foo: 'bar'}); // <-- fails
Not available in React's propTypes
This can only be used in combination with shape.requiredIfNot
1checker = checkers.shape({ 2 foobar: checkers.shape.requiredIfNot.all(['foobaz', 'baz'], checkers.bool), 3 foobaz: checkers.object.optional, 4 baz: checkers.string.optional, 5 foo: checkers.string.optional 6}); 7checker({ 8 foo: [1, 2] 9}); // <-- fails 10 11checker({ 12 foo: [1, 2], 13 foobar: true 14}); // <-- passes 15 16checker({ 17 foo: [1, 2], 18 baz: 'foo' 19}); // <-- passes
Not available in React's propTypes
This will check if the given item is an arguments
-like object (non-array object that has a length property)
1function foo(bar) { 2 apiCheck.args(arguments); // <-- pass 3} 4apiCheck.args([]); // <-- fail 5apiCheck.args({}); // <-- fail 6apiCheck.args({length: 3}); // <-- pass 7apiCheck.args({length: 'not-number'}); // <-- fail
1apiCheck.any({}); // <-- pass 2apiCheck.any([]); // <-- pass 3apiCheck.any(true); // <-- pass 4apiCheck.any(false); // <-- pass 5apiCheck.any(/* seriously, anything, except undefined */); // <-- fail 6apiCheck.any.optional(/* unless you specify optional :-) */); // <-- pass 7apiCheck.any(3); // <-- pass 8apiCheck.any(3.1); // <-- pass 9apiCheck.any(3.14); // <-- pass 10apiCheck.any(3.141); // <-- pass 11apiCheck.any(3.1415); // <-- pass 12apiCheck.any(3.14159); // <-- pass 13apiCheck.any(3.141592); // <-- pass 14apiCheck.any(3.1415926); // <-- pass 15apiCheck.any(3.14159265); // <-- pass 16apiCheck.any(3.141592653); // <-- pass 17apiCheck.any(3.1415926535); // <-- pass 18apiCheck.any(3.14159265359); // <-- pass 19apiCheck.any(jfio,.jgo); // <-- Syntax error.... ಠ_ಠ
You can specify your own type. You do so like so:
1var myCheck = require('api-check')({ 2 output: {prefix: 'myCheck'} 3}); 4 5function ipAddressChecker(val, name, location) { 6 if (!/(\d{1,3}\.){3}\d{1,3}/.test(val)) { 7 return apiCheck.utils.getError(name, location, ipAddressChecker.type); 8 } 9}; 10ipAddressChecker.type = 'ipAddressString'; 11 12function foo(string, ipAddress) { 13 myCheck.warn([ 14 myCheck.string, 15 ipAddressChecker 16 ], arguments); 17}
Then, if you invoked that function like this:
1foo('hello', 'not-an-ip-address');
It would result in a warning like this:
myCheck apiCheck failed! `Argument 1` passed, `value` at `Argument 2` must be `ipAddressString`
You passed:
[
"hello",
"not-an-ip-address"
]
With the types:
[
"string",
"string"
]
The API calls for:
[
"String",
"ipAddressString"
]
There's actually quite a bit of cool stuff you can do with custom types checkers. If you want to know what they are, look at the tests or file an issue for me to go document them. :-)
When writing custom types, you may find the utils
helpful. Please file an issue to ask me to improve documentation for
what's available. For now, check out api-check-utils.test.js
Note, obviously, these things are specific to apiCheck
and not part of React propTypes
When you create your instance of apiCheck
, you can configure it with different options as part of the first argument.
You can specify some extra options for the output of the message.
1var myApiCheck = require('api-check')({ 2 output: { 3 prefix: 'Global prefix', 4 suffix: 'global suffix', 5 docsBaseUrl: 'https://example.com/errors-and-warnings#' 6 }, 7 verbose: false, // <-- defaults to false 8 disabled: false // <-- defaults to false, set this to true in production 9});
You can also specify an output
object to each apiCheck()
, apiCheck.throw()
, and apiCheck.warn()
request:
1myApiCheck(apiCheck.bool, arguments, { 2 prefix: 'instance prefix:', 3 suffix: 'instance suffix', 4 urlSuffix: 'example-error-additional-info' 5});
A failure with the above configuration would yield something like this:
Global prefix instance prefix {{error message}} instance suffix global suffix https://example.com/errors-and-warnings#example-error-additional-info
As an alternative to urlSuffix
, you can also specify a url
:
1myApiCheck(apiCheck.bool, arguments, { 2 url: 'https://example.com/some-direct-url-that-does-not-use-the-docsBaseUrl' 3});
This is the method that apiCheck uses to get the message it throws or console.warns. If you don't like it, feel free to
make a better one by simply: apiCheck.getErrorMessage = function(api, args, output) {/* return message */}
This is the method that apiCheck uses to throw or warn the message. If you prefer to do your own thing, that's cool.
Simply apiCheck.handleErrorMessage = function(message, shouldThrow) { /* throw or warn */ }
It's a good idea to disable the apiCheck in production. You can disable your own instance of apiCheck
as part of
the options
, but it's probably just better to disable apiCheck
globally. I recommend you do this before you (or
any of you dependencies) create an instance of apiCheck
. Here's how you would do that:
1var apiCheck = require('api-check'); 2apiCheck.globalConfig.disabled = true;
This library was written by Kent C. Dodds. Again, big credits go to the team working on React for thinking up the api. This library was written from scratch, but I'd be lying if I didn't say that I referenced their functions a time or two.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More