This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -94,18 +94,24 @@ SchemaBoolean.cast = function cast(caster) {
return this._cast;
}
if (caster === false) {
caster = v => {
if (v != null && typeof v !== 'boolean') {
throw new Error();
}
return v;
};
caster = this._defaultCaster;
}
this._cast = caster;
return this._cast;
};
/*!
* ignore
*/
SchemaBoolean._defaultCaster = v => {
if (v != null && typeof v !== 'boolean') {
throw new Error();
}
return v;
};
/*!
* ignore
*/
@@ -188,9 +194,15 @@ Object.defineProperty(SchemaBoolean, 'convertToFalse', {
*/
SchemaBoolean.prototype.cast = function(value) {
const castBoolean = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaBoolean.cast();
let castBoolean;
if (typeof this._castFunction === 'function') {
castBoolean = this._castFunction;
} else if (typeof this.constructor.cast === 'function') {
castBoolean = this.constructor.cast();
} else {
castBoolean = SchemaBoolean.cast();
}
try {
return castBoolean(value);
} catch (error) {
@@ -224,6 +236,33 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val) {
return this._castForQuery($conditional);
};
/**
*
* @api private
*/
SchemaBoolean.prototype._castNullish = function _castNullish(v) {
if (typeof v === 'undefined' &&
this.$$context != null &&
this.$$context._mongooseOptions != null &&
this.$$context._mongooseOptions.omitUndefined) {
return v;
}
const castBoolean = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaBoolean.cast();
if (castBoolean == null) {
return v;
}
if (castBoolean.convertToFalse instanceof Set && castBoolean.convertToFalse.has(v)) {
return false;
}
if (castBoolean.convertToTrue instanceof Set && castBoolean.convertToTrue.has(v)) {
return true;
}
return v;
};
/*!
* Module exports.
*/