altheia-async-data-validator
Version:
A very simple, fast and customizable async data validator
79 lines (78 loc) • 1.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeBoolean = exports.messages = void 0;
const base_1 = require("./base");
exports.messages = {
'boolean.typeof': (name) => `${name} must be a valid boolean`,
'boolean.true': (name) => `${name} must be true`,
'boolean.false': (name) => `${name} must be false`,
};
/**
* Boolean class
*/
class TypeBoolean extends base_1.TypeBase {
/**
* Constructor
*/
constructor() {
super();
this.name = 'boolean';
this.typeof();
}
/**
* Try to cast value
*
* @param {mixed} value
* @return {boolean|mixed}
*/
_cast(value) {
if (typeof value === 'string') {
if (value === 'true' || value === 'True') {
return true;
}
if (value === 'false' || value === 'False') {
return false;
}
}
return value;
}
/**
* Test to validate the type of the value
*
* @return {this}
*/
typeof() {
this.test('typeof', (bool) => {
return typeof bool === 'boolean';
});
return this;
}
/**
* Force a boolean to equal true
*
* @return {this}
*/
true() {
this.test('true', (bool) => {
return bool === true;
});
return this;
}
/**
* Force a boolean to equal false
*
* @return {this}
*/
false() {
this.test('false', (bool) => {
return bool === false;
});
return this;
}
}
exports.TypeBoolean = TypeBoolean;
const def = {
Class: TypeBoolean,
messages: exports.messages,
};
exports.default = def;
;