check-type
Version:
Library to check variable type and properties in object.
348 lines (231 loc) • 9.12 kB
Markdown
//travis-ci.org/alistairjcbrown/check-type.svg?branch=master)](https://travis-ci.org/alistairjcbrown/check-type)
[](http://badge.fury.io/gh/alistairjcbrown%2Fcheck-type)
[](https://david-dm.org/alistairjcbrown/check-type)
[](https://david-dm.org/alistairjcbrown/check-type#info=devDependencies)
A type checking framework for Javascript.
Check type lets you check variable types and structures with the format `check(x).is("y")` and `check(x).has("a.b")`
---
* [Dependencies](
* [Installation](
* [Node.js](
* [Bower](
* [Component](
* [Manual](
* [Use](
* [Node.js](
* [Browser](
* [RequireJS](
* [Testing](
* [Test in Node.js](
* [Test in Browser](
* [No Conflict](
* [Init](
* [Simple use](
* [More complex use](
* [Clear](
* [Type checking](
* [Example: Checking for string using `is`](
* [Example: Checking for string using `is.not`](
* [Object path checking](
* [Example: Checking object path using `has`](
* [Object structure checking](
* [Example: Checking object properties using `matches`](
* [Complex example](
* [Set up `check-type`](
* [Retrieve username and password from authentication request](
* [Contact](
---
* [Underscore.js](http://underscorejs.org/).
`check-type` is available through the [npm package manager](https://npmjs.org/package/check-type).
[](http://badge.fury.io/js/check-type)
For Node.js package management, you can install [using npm](https://www.npmjs.org/).
```
npm install check-type
```
`check-type` is available through the [bower package manager](http://bower.io/search/?q=check-type).
[](http://badge.fury.io/bo/check-type)
For front-end package management, you can install [using Bower](http://bower.io/).
```
bower install check-type
```
`check-type` is available through the [component package manager](http://component.io/alistairjcbrown/check-type).
For package management, you can install [using Component](https://github.com/component/component#installation).
```
component install alistairjcbrown/check-type
```
For manual management, you can grab the Javascript file directly or clone the git repository.
You will need to grab `underscore.js` as well.
```
wget https://raw.githubusercontent.com/alistairjcbrown/check-type/master/check-type.min.js
git clone git@github.com:alistairjcbrown/check-type.git
```
```js
var check = require("check-type").init();
```
The module can be required using Node.js built in `require` ([See example](lib/examples/nodejs)).
```html
<script src="/path/to/underscore"></script>
<script src="/path/to/check-type/check-type.min.js"></script>
<script>
check.init();
</script>
```
The module can be used in the browser through the `<script>` tag and will bind to `window` ([See example](lib/examples/browser)).
The module supports the AMD format and uses `define` if available. Therefore it can be used as a RequireJS module ([See Browser example](lib/examples/requirejs/browser), [See Node.js example](lib/examples/requirejs/nodejs)).
```js
define([ "check-type" ], function(check) {
check.init();
});
```
Built in tests and linting using [Grunt](http://gruntjs.com/) to call [JSHint](http://www.jshint.com/about/) and [Mocha](http://visionmedia.github.io/mocha/).
Get all of the developer dependecies by running:
```
npm install
```
```
grunt lint
grunt test --nodejs
grunt test --browser
grunt test
grunt go
```
Open `lib/test/check-type.test.html` in browser
To prevent conlicts with other libraries using `window.check`, if `check-type` binds to `window` it will also provide a `check.noConflict` function to restore the variable to its previous value.
`check-type` will only bind to `window` in the browser environment when RequireJS is not available.
`check-type` does not come with type checking functionality. Instead, it simply provides the `check` interface. Type checking functions should be provided when calling `check.init`.
`check.init` can be called without parameters which will use the type checking functions from [Underscore.js](http://underscorejs.org/).
`check.init` can be called multiple times and will add type checking functions which have not already been defined.
To override a previously defined type checking function, pass boolean `true` as the second parameter.
```js
var check = require("check-type").init();
```
```js
var check = require("check-type"),
custom_functions = {
"isEmail": function(value) {
return value.indexOf("@") !== -1
},
"isEmpty": function(value) {
return value === "empty";
}
},
// Initialise check with underscore type checking functions
// and custom checking functions, overriding underscore's isEmpty function
check.init()
.init(custom_functions, true);
```
This clears all of the internal stored type checking functions.
```js
check.clear();
```
Once the `check` function has been initialised, it can utilise any defined type checking functions using `is`.
```js
var my_string = "hello world";
check.init();
check(my_string).is("string"); // true
check(my_string).is("number"); // false
check(my_string).is("foo"); // throws Error for unsupported type
```
You can also negate the check with `is.not`
```js
var my_string = "hello world";
check.init();
check(my_string).is.not("string"); // false
check(my_string).is.not("number"); // true
check(my_string).is.not("foo"); // throws Error for unsupported type
```
`check-type` can check for the presence of values within an object under a particular path.
```js
var my_object = {
hello: {
world: false
}
};
check(my_object).has("hello.world"); // true
check(my_object).has("foo.bar"); // false
```
`check-type` can check an object properties for specific types
```js
var my_object = {
"customer_number": 123456789,
"password": "abc123"
};
check(my_object).matches({
"customer_number": "number",
"password": "string"
});
// true
check(my_object).matches({
"username": "string",
"password": "string"
});
// false
```
The functionality of `check` can be used in combination, for example when validating response data.
See the example below as a [jsfiddle](http://jsfiddle.net/alistairjcbrown/B9AHu/).
```js
var custom_types = {};
custom_types.isUsername = function(value) {
return /^\w+$/.test(value);
};
custom_types.isAuthObject = function(value) {
return check(value).matches({
"username": "username",
"password": "string"
});
};
// Initialise with underscorejs functions
check.init();
// Add custom functions too
check.init(custom_types);
```
```js
function handleAuthentication(request) {
var username, password;
if (check(request).has("auth") &&
check(request.auth).is("AuthObject")) {
username = request.auth.username;
password = request.auth.password;
return {
username: username,
password: password
};
}
return false;
}
```
Twitter [@alistairjcbrown](http://twitter.com/alistairjcbrown)
Code signed using [keybase](SIGNED.md) as [alistairjcbrown](https://keybase.io/alistairjcbrown). Verify with `keybase dir verify`
[![Build Status](https: