SchemaBoolean
SchemaBoolean()SchemaBoolean.checkRequired()SchemaBoolean.convertToFalseSchemaBoolean.convertToTrueSchemaBoolean.get()SchemaBoolean.get()SchemaBoolean.prototype.checkRequired()SchemaBoolean.schemaNameSchemaBoolean.set()
SchemaBoolean()
参数
path«String»options«Object»
继承
布尔 SchemaType 构造函数。
SchemaBoolean.checkRequired()
参数
fn«Function»
返回值
- «Function»
类型
- «property»
覆盖必需验证器用于检查布尔值是否通过required检查的函数。
SchemaBoolean.convertToFalse
类型
- «Set»
配置哪些值会被转换为false。
示例
const M = mongoose.model('Test', new Schema({ b: Boolean }));
new M({ b: 'nay' }).b; // undefined
mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
new M({ b: 'nay' }).b; // false SchemaBoolean.convertToTrue
类型
- «Set»
配置哪些值会被转换为true。
示例
const M = mongoose.model('Test', new Schema({ b: Boolean }));
new M({ b: 'affirmative' }).b; // undefined
mongoose.Schema.Boolean.convertToTrue.add('affirmative');
new M({ b: 'affirmative' }).b; // true SchemaBoolean.get()
参数
getter«Function»
返回值
- «this»
类型
- «property»
为所有布尔实例附加 getter。
示例
mongoose.Schema.Boolean.get(v => v === true ? 'yes' : 'no');
const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
new Order({ isPaid: false }).isPaid; // 'no' SchemaBoolean.get()
参数
caster«Function»
返回值
- «Function»
类型
- «property»
获取/设置用于将任意值转换为布尔值的函数。
示例
// Make Mongoose cast empty string '' to false.
const original = mongoose.Schema.Boolean.cast();
mongoose.Schema.Boolean.cast(v => {
if (v === '') {
return false;
}
return original(v);
});
// Or disable casting entirely
mongoose.Schema.Boolean.cast(false); SchemaBoolean.prototype.checkRequired()
参数
value«Any»
返回值
- «Boolean»
检查给定值是否满足必需验证器。对于布尔值而言,要满足必需验证器,它必须严格等于 true 或 false。
SchemaBoolean.schemaName
类型
- «property»
此模式类型的名称,用于防御混淆函数名的压缩器。
SchemaBoolean.set()
参数
option«String» 你想设置值的选项value«Any» 选项的值
返回值
- «undefined,void»
类型
- «property»
为所有布尔实例设置默认选项。
示例
// Make all booleans have `default` of false.
mongoose.Schema.Boolean.set('default', false);
const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
new Order({ }).isPaid; // false 
