mongodb Mongoose模型枚举的自定义错误消息

wvt8vs2t  于 2022-11-03  发布在  Go
关注(0)|答案(6)|浏览(151)

我想自定义Mongoose模型生成的验证消息。
我倾向于不直接对模式对象进行验证(例如,required),因为没有自定义错误消息的自由。

sourceAccountId: {
  type: Schema.ObjectId,
  require: true,
  ref: 'Account'
}

而是执行以下操作。

sourceAccountId: {
  type: Schema.ObjectId,
  ref: 'Account'
}

ConnectionRequestSchema.path('sourceAccountId').required(true, 'Source Account is required.');

当字段具有枚举约束时,我无法找到覆盖默认枚举消息的方法。
下面列出了我的模型,状态验证消息对required正常工作,但对enum不正常。

'use strict';

var _ = require('lodash');

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var ConnectionRequestSchema = new Schema({
  created_at: { type: Date },
  updated_at: { type: Date },

  sourceAccountId: {
    type: Schema.ObjectId,
    ref: 'Account'
  },

  status: {
    type: String,
    enum: ['pending', 'accept', 'decline'],
    trim: true
  }
});

// ------------------------------------------------------------
// Validations
// ------------------------------------------------------------

ConnectionRequestSchema.path('sourceAccountId').required(true, 'Source Account is required.');
ConnectionRequestSchema.path('status').required(true, 'Status is required.');
//ConnectionRequestSchema.path('status').enum(['pending', 'accept', 'decline'], 'Status is invalid, valid values include [pending, accept, decline]');

// ------------------------------------------------------------
// Save
// ------------------------------------------------------------

ConnectionRequestSchema.pre('save', function (next) {
  var now = new Date().getTime();

  this.updated_at = now;
  if (!this.created_at) {
    this.created_at = now;
  }

  next();
});

module.exports = mongoose.model('ConnectionRequest', ConnectionRequestSchema);
vuktfyat

vuktfyat1#

尝试类似的方式:

var enu = {
  values: ['pending', 'accept', 'decline']
, message: 'Status is required.'
}

var ConnectionRequestSchema = new Schema({
  ...

  status: {
    type: String
  , enum: enu
  , trim: true
  }
});
pgccezyw

pgccezyw2#

difficulty: {
        type: String,
        required: [true, 'A tour must have a difficulty.'],
        enum: {
            values: ['easy', 'medium', 'difficult'],
            message: 'Difficult is either easy, medium, difficult.'
        }
    }
ohtdti5x

ohtdti5x3#

这应该可行:

var ConnectionRequestSchema = new Schema({
  ...

  status: {
    type: String,
    enum: {values: ['pending', 'accept', 'decline'], message: 'Status is required.'},
    trim: true
  },
  ...
});
zsbz8rwp

zsbz8rwp4#

const tourSchema = new mongoose.Schema(
  {
    difficulty: {
      type: String,
      required: [true, 'A tour must have a difficulty'],
      enum: {
        // works only for string
        values: ['easy', 'medium', 'difficult'],
        message: 'Difficulty level is either: easy, medium, or difficult',
      },
    }
  }
);
svujldwt

svujldwt5#

Because you should write like that . Error is type value.

status: {
    type: [String], 
    enum: ["pending", "success", "err"],
    trim: true
}

will be fixed.

1rhkuytd

1rhkuytd6#

你也可以这么做

status: {
        type: String,
        enum: ["pending", "success", "error"],
        trim: true
    }

相关问题