UNPKG

1.04 kBJavaScriptView Raw
1/**
2 * Module dependencies.
3 */
4
5var _ = require('lodash'),
6 SchemaType = require('../schematype');
7
8/**
9 * Creates a new SchemaBoolean instance.
10 *
11 * @param {Object} [options]
12 * @api public
13 */
14
15var SchemaBoolean = module.exports = function(options){
16 SchemaType.call(this, options);
17};
18
19SchemaBoolean.type = SchemaBoolean.prototype.type = Boolean;
20
21/**
22 * Inherits from SchemaType.
23 */
24
25SchemaBoolean.__proto__ = SchemaType;
26SchemaBoolean.prototype.__proto__ = SchemaType.prototype;
27
28/**
29 * Checks if the given `value` is a boolean.
30 *
31 * @param {any} value
32 * @return {Boolean}
33 * @api public
34 */
35
36SchemaBoolean.prototype.checkRequired = function(value){
37 return value === true || value === false;
38};
39
40/**
41 * Casts the given `value` to a boolean.
42 *
43 * @param {any} value
44 * @return {Boolean}
45 * @api public
46 */
47
48SchemaBoolean.prototype.cast = function(value){
49 if (value == null) return value;
50 if ('0' === value) return false;
51 if ('true' === value) return true;
52 if ('false' === value) return false;
53
54 return !!value;
55};
\No newline at end of file