ottoman
Version:
Ottoman Couchbase ODM
72 lines • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.booleanTypeFactory = exports.BooleanType = void 0;
const core_type_1 = require("./core-type");
const cast_strategy_1 = require("../../utils/cast-strategy");
/**
* `Boolean` are plain JavaScript Boolean.
*
* ## Options
*
* - **required** flag to define if the field is mandatory
* - **validator** that will be applied to the field a validation function, validation object or string with the name of the custom validator
* - **default** that will define the initial value of the field, this option allows a value or a function
* - **immutable** that will define this field as immutable. Ottoman prevents you from changing immutable fields if the schema as configure like strict
*
*
* @example
* ```typescript
* const userSchema = new Schema({
* isActive: Boolean,
* isSomething: Schema.Types.Boolean
* })
* ```
*
* ### Ottoman will cast the following values to true:
* * true
* * 'true'
* * 1
* * '1'
* * 'yes'
*
* ### Ottoman will cast the following values to false:
* * false
* * 'false'
* * 0
* * '0'
* * 'no'
*/
class BooleanType extends core_type_1.CoreType {
constructor(name, options) {
super(name, BooleanType.sName, options);
}
cast(value, strategy = cast_strategy_1.CAST_STRATEGY.DEFAULT_OR_DROP) {
if (BooleanType.convertToTrue.has(value)) {
return true;
}
else if (BooleanType.convertToFalse.has(value)) {
return false;
}
else {
return (0, cast_strategy_1.checkCastStrategy)(value, strategy, this);
}
}
validate(value, strategy) {
value = super.validate(value, strategy);
const _value = this.cast(value, strategy);
if (_value === undefined || _value === null)
return value;
this.checkValidator(value);
return _value;
}
isEmpty(value) {
return value === undefined || value === null;
}
}
exports.BooleanType = BooleanType;
BooleanType.sName = Boolean.name;
BooleanType.convertToTrue = new Set([true, 'true', 1, '1', 'yes']);
BooleanType.convertToFalse = new Set([false, 'false', 0, '0', 'no']);
const booleanTypeFactory = (key, opts) => new BooleanType(key, opts);
exports.booleanTypeFactory = booleanTypeFactory;
//# sourceMappingURL=boolean-type.js.map