SchemaBuffer


SchemaBuffer()

參數
  • key «字串»
  • options «物件»
繼承

Buffer SchemaType 建構子


SchemaBuffer.checkRequired()

參數
  • fn «函式»
回傳
  • «函式»
類型
  • «屬性»

覆寫 required 驗證器用來檢查字串是否通過 required 檢查的函式。

範例

// Allow empty strings to pass `required` check
mongoose.Schema.Types.String.checkRequired(v => v != null);

const M = mongoose.model({ buf: { type: Buffer, required: true } });
new M({ buf: Buffer.from('') }).validateSync(); // validation passes!

SchemaBuffer.get()

參數
  • getter «函式»
回傳
  • «this»
類型
  • «屬性»

為所有 Buffer 實例附加一個 getter

範例

// Always convert to string when getting an ObjectId
mongoose.Schema.Types.Buffer.get(v => v.toString('hex'));

const Model = mongoose.model('Test', new Schema({ buf: Buffer } }));
typeof (new Model({ buf: Buffer.fromString('hello') }).buf); // 'string'

SchemaBuffer.prototype.checkRequired()

參數
  • value «任意»
  • doc «文檔»
回傳
  • «布林值»

檢查給定的值是否滿足 required 驗證器。為了滿足 required 驗證器,緩衝區不得為 null 或未定義,且長度不得為零。


SchemaBuffer.prototype.subtype()

參數
  • subtype «數字» 預設的子類型

回傳
  • «SchemaType» this

為此緩衝區設定預設的 子類型。您可以在這裡找到允許的子類型列表

範例

const s = new Schema({ uuid: { type: Buffer, subtype: 4 });
const M = db.model('M', s);
const m = new M({ uuid: 'test string' });
m.uuid._subtype; // 4

SchemaBuffer.schemaName

類型
  • «屬性»

此綱要類型的名稱,以防止縮小器混淆函式名稱。


SchemaBuffer.set()

參數
  • option «字串» 您想要設定值的選項

  • value «任意» 選項的值

回傳
  • «undefined,void»
類型
  • «屬性»

為所有 Buffer 實例設定預設選項。

範例

// Make all buffers have `required` of true by default.
mongoose.Schema.Buffer.set('required', true);

const User = mongoose.model('User', new Schema({ test: Buffer }));
new User({ }).validateSync().errors.test.message; // Path `test` is required.