SchemaNumber


SchemaNumber()

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

Number SchemaType 建構子。


SchemaNumber.checkRequired()

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

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


SchemaNumber.get()

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

為所有 Number 實例附加一個 getter。

範例

// Make all numbers round down
mongoose.Number.get(function(v) { return Math.floor(v); });

const Model = mongoose.model('Test', new Schema({ test: Number }));
new Model({ test: 3.14 }).test; // 3

SchemaNumber.get()

參數
  • caster «函式»
返回
  • «函式»
類型
  • «屬性»

取得/設定用於將任意值轉換為數字的函式。

範例

// Make Mongoose cast empty strings '' to 0 for paths declared as numbers
const original = mongoose.Number.cast();
mongoose.Number.cast(v => {
  if (v === '') { return 0; }
  return original(v);
});

// Or disable casting entirely
mongoose.Number.cast(false);

SchemaNumber.prototype.checkRequired()

參數
  • value «任意»
  • doc «文件»
返回
  • «布林值»

檢查給定的值是否滿足 required 驗證器。


SchemaNumber.prototype.enum()

參數
  • values «陣列» 允許的值

  • [message] «字串» 可選的自訂錯誤訊息

返回
  • «SchemaType» this
參見

設定一個 enum 驗證器

範例

const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
const M = db.model('M', s);

const m = new M({ n: 4 });
await m.save(); // throws validation error

m.n = 3;
await m.save(); // succeeds

SchemaNumber.prototype.max()

參數
  • maximum «數字» 數字

  • [message] «字串» 可選的自訂錯誤訊息

返回
  • «SchemaType» this
參見

設定一個最大數字驗證器。

範例

const s = new Schema({ n: { type: Number, max: 10 })
const M = db.model('M', s)
const m = new M({ n: 11 })
m.save(function (err) {
  console.error(err) // validator error
  m.n = 10;
  m.save() // success
})

// custom error messages
// We can also use the special {MAX} token which will be replaced with the invalid value
const max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
const schema = new Schema({ n: { type: Number, max: max })
const M = mongoose.model('Measurement', schema);
const s= new M({ n: 4 });
s.validate(function (err) {
  console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
})

SchemaNumber.prototype.min()

參數
  • value «數字» 最小數字

  • [message] «字串» 可選的自訂錯誤訊息

返回
  • «SchemaType» this
參見

設定一個最小數字驗證器。

範例

const s = new Schema({ n: { type: Number, min: 10 })
const M = db.model('M', s)
const m = new M({ n: 9 })
m.save(function (err) {
  console.error(err) // validator error
  m.n = 10;
  m.save() // success
})

// custom error messages
// We can also use the special {MIN} token which will be replaced with the invalid value
const min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
const schema = new Schema({ n: { type: Number, min: min })
const M = mongoose.model('Measurement', schema);
const s= new M({ n: 4 });
s.validate(function (err) {
  console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
})

SchemaNumber.schemaName

類型
  • «屬性»

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


SchemaNumber.set()

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

  • value «任意» 選項的值

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

為所有 Number 實例設定一個預設選項。

範例

// Make all numbers have option `min` equal to 0.
mongoose.Schema.Number.set('min', 0);

const Order = mongoose.model('Order', new Schema({ amount: Number }));
new Order({ amount: -10 }).validateSync().errors.amount.message; // Path `amount` must be larger than 0.