Mongoose Model Custom Error Message for Enums

这一生的挚爱 提交于 2020-08-27 05:43:17

问题


I would like to customize the validation messages that my Mongoose Models produce.

I tend to NOT put my validations (e.g. required) on the schema object directly because there is no freedom to have custom error messages. e.g.

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

instead I do the following.

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

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

I have been unable to find a way to override the default enum message when a field has enum constraints.

My Model is Listed Below, with the status validation message working fine for required, but not for 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);

回答1:


Try something like it:

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


var ConnectionRequestSchema = new Schema({
  ...

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



回答2:


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



回答3:


This should work:

var ConnectionRequestSchema = new Schema({
  ...

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



回答4:


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',
      },
    }
  }
);


来源:https://stackoverflow.com/questions/37403907/mongoose-model-custom-error-message-for-enums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!