David Cruwys
David Cruwys

Reputation: 6872

Mongoose Model Custom Error Message for Enums

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);

Upvotes: 11

Views: 12383

Answers (6)

Tolgahanbora
Tolgahanbora

Reputation: 1

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

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

will be fixed.

Upvotes: 0

Lawrence Kwis
Lawrence Kwis

Reputation: 9

You could do this too

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

Upvotes: -1

zaib
zaib

Reputation: 69

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

Upvotes: 6

Rafiq
Rafiq

Reputation: 11535

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

Upvotes: 0

redemma71
redemma71

Reputation: 1

This should work:

var ConnectionRequestSchema = new Schema({
  ...

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

Upvotes: 0

andergtk
andergtk

Reputation: 420

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

Upvotes: 25

Related Questions