mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
93 lines
1.5 KiB
JavaScript
93 lines
1.5 KiB
JavaScript
![]() |
/*!
|
||
|
* Module dependencies.
|
||
|
*/
|
||
|
|
||
|
var SchemaType = require('../schematype');
|
||
|
|
||
|
/**
|
||
|
* Boolean SchemaType constructor.
|
||
|
*
|
||
|
* @param {String} path
|
||
|
* @param {Object} options
|
||
|
* @inherits SchemaType
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
function SchemaBoolean (path, options) {
|
||
|
SchemaType.call(this, path, options);
|
||
|
};
|
||
|
|
||
|
/*!
|
||
|
* Inherits from SchemaType.
|
||
|
*/
|
||
|
SchemaBoolean.prototype.__proto__ = SchemaType.prototype;
|
||
|
|
||
|
/**
|
||
|
* Required validator
|
||
|
*
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
SchemaBoolean.prototype.checkRequired = function (value) {
|
||
|
return value === true || value === false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Casts to boolean
|
||
|
*
|
||
|
* @param {Object} value
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
SchemaBoolean.prototype.cast = function (value) {
|
||
|
if (null === value) return value;
|
||
|
if ('0' === value) return false;
|
||
|
if ('true' === value) return true;
|
||
|
if ('false' === value) return false;
|
||
|
return !! value;
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
* ignore
|
||
|
*/
|
||
|
|
||
|
function handleArray (val) {
|
||
|
var self = this;
|
||
|
return val.map(function (m) {
|
||
|
return self.cast(m);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
SchemaBoolean.$conditionalHandlers = {
|
||
|
'$in': handleArray
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Casts contents for queries.
|
||
|
*
|
||
|
* @param {String} $conditional
|
||
|
* @param {any} val
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
SchemaBoolean.prototype.castForQuery = function ($conditional, val) {
|
||
|
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;
|