SchemaNumber


SchemaNumber()

パラメータ
  • key «文字列»
  • options «オブジェクト»
継承

Number SchemaType コンストラクタ。


SchemaNumber.checkRequired()

パラメータ
  • fn «関数»
戻り値
  • «関数»
  • «プロパティ»

必須バリデータが文字列がrequiredチェックに合格するかどうかをチェックするために使用する関数をオーバーライドします。


SchemaNumber.get()

パラメータ
  • getter «関数»
戻り値
  • «this»
  • «プロパティ»

すべてのNumberインスタンスにゲッターをアタッチします。

// 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 «ドキュメント»
戻り値
  • «ブール値»

指定された値が必須バリデータに適合するかどうかをチェックします。


SchemaNumber.prototype.enum()

パラメータ
  • values «配列» 許容値

  • [message] «文字列» オプションのカスタムエラーメッセージ

戻り値
  • «SchemaType» this
参照

列挙型バリデータを設定します。

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.