SchemaArray
SchemaArray()
SchemaArray.checkRequired()
SchemaArray.get()
SchemaArray.options
SchemaArray.prototype.checkRequired()
SchemaArray.prototype.enum()
SchemaArray.schemaName
SchemaArray.set()
SchemaArray()
參數
key
«字串»cast
«SchemaType»options
«物件»schemaOptions
«物件»
繼承自
Array SchemaType 建構子
SchemaArray.checkRequired()
參數
fn
«函式»
返回
- «函式»
覆寫 required 驗證器用來檢查陣列是否通過 required
檢查的函式。
範例
// Require non-empty array to pass `required` check
mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);
const M = mongoose.model({ arr: { type: Array, required: true } });
new M({ arr: [] }).validateSync(); // `null`, validation fails!
SchemaArray.get()
參數
getter
«函式»
返回
- «this»
類型
- «屬性»
為所有 Array 實例附加 getter
SchemaArray.options
類型
- «屬性»
所有陣列的選項。
castNonArrays
:預設為true
。如果為false
,當值不是陣列時,Mongoose 會拋出 CastError。如果為true
,Mongoose 會在轉換之前將提供的值包裝在陣列中。
SchemaArray.prototype.checkRequired()
參數
value
«任何»doc
«文件»
返回
- «布林值»
檢查給定的值是否滿足 required
驗證器。
SchemaArray.prototype.enum()
參數
[...args]
«字串|物件» 列舉值
返回
- «SchemaArray» this
如果這是字串或數字的陣列,則新增列舉驗證器。等同於 SchemaString.prototype.enum()
或 SchemaNumber.prototype.enum()
SchemaArray.schemaName
類型
- «屬性»
此 schema 類型的名稱,以防禦會破壞函式名稱的最小化器。
SchemaArray.set()
參數
option
«字串» 您想要設定其值的選項value
«任何» 選項的值
返回
- «undefined,void»
為所有 Array 實例設定預設選項。
範例
// Make all Array instances have `required` of true by default.
mongoose.Schema.Array.set('required', true);
const User = mongoose.model('User', new Schema({ test: Array }));
new User({ }).validateSync().errors.test.message; // Path `test` is required.