SchemaBoolean
SchemaBoolean()
SchemaBoolean.checkRequired()
SchemaBoolean.convertToFalse
SchemaBoolean.convertToTrue
SchemaBoolean.get()
SchemaBoolean.get()
SchemaBoolean.prototype.checkRequired()
SchemaBoolean.schemaName
SchemaBoolean.set()
SchemaBoolean()
參數
path
«字串»options
«物件»
繼承
布林值 SchemaType 建構子。
SchemaBoolean.checkRequired()
參數
fn
«函式»
回傳
- «函式»
型別
- «屬性»
覆寫 required 驗證器用來檢查布林值是否通過 required
檢查的函式。
SchemaBoolean.convertToFalse
型別
- «設定»
設定哪些值會被轉換為 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
型別
- «設定»
設定哪些值會被轉換為 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
«函式»
回傳
- «this»
型別
- «屬性»
為所有布林值實例附加一個 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
«函式»
回傳
- «函式»
型別
- «屬性»
取得/設定用於將任意值轉換為布林值的函式。
範例
// 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
«任何»
回傳
- «布林值»
檢查給定的值是否滿足 required 驗證器。對於要滿足 required 驗證器的布林值,它必須嚴格等於 true 或 false。
SchemaBoolean.schemaName
型別
- «屬性»
此 schema type 的名稱,以防止縮小器混淆函式名稱。
SchemaBoolean.set()
參數
option
«字串» 您想要設定值的選項value
«任何» 選項的值
回傳
- «undefined,void»
型別
- «屬性»
為所有布林值實例設定一個預設選項。
範例
// 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