Files

92 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2014-09-14 07:04:16 -04:00
/*!
* Module dependencies.
*/
2015-11-24 22:08:58 -08:00
var utils = require('../utils');
2014-09-14 07:04:16 -04:00
var SchemaType = require('../schematype');
/**
* Boolean SchemaType constructor.
*
* @param {String} path
* @param {Object} options
* @inherits SchemaType
* @api private
*/
2015-11-24 22:08:58 -08:00
function SchemaBoolean(path, options) {
SchemaType.call(this, path, options, 'Boolean');
}
/**
* This schema type's name, to defend against minifiers that mangle
* function names.
*
* @api private
*/
SchemaBoolean.schemaName = 'Boolean';
2014-09-14 07:04:16 -04:00
/*!
* Inherits from SchemaType.
*/
2015-11-24 22:08:58 -08:00
SchemaBoolean.prototype = Object.create( SchemaType.prototype );
SchemaBoolean.prototype.constructor = SchemaBoolean;
2014-09-14 07:04:16 -04:00
/**
* Required validator
*
* @api private
*/
2015-11-24 22:08:58 -08:00
SchemaBoolean.prototype.checkRequired = function(value) {
2014-09-14 07:04:16 -04:00
return value === true || value === false;
};
/**
* Casts to boolean
*
* @param {Object} value
* @api private
*/
2015-11-24 22:08:58 -08:00
SchemaBoolean.prototype.cast = function(value) {
2014-09-14 07:04:16 -04:00
if (null === value) return value;
if ('0' === value) return false;
if ('true' === value) return true;
if ('false' === value) return false;
2015-11-24 22:08:58 -08:00
return !!value;
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
SchemaBoolean.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {});
2014-09-14 07:04:16 -04:00
/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} val
* @api private
*/
2015-11-24 22:08:58 -08:00
SchemaBoolean.prototype.castForQuery = function($conditional, val) {
2014-09-14 07:04:16 -04:00
var handler;
if (2 === arguments.length) {
handler = SchemaBoolean.$conditionalHandlers[$conditional];
if (handler) {
return handler.call(this, val);
}
return this.cast(val);
}
return this.cast($conditional);
};
/*!
* Module exports.
*/
module.exports = SchemaBoolean;