skip to Main Content
      slider_value: {
        type: Number,
        required: false,
      },

This is the Mongoose schema for one of the fields in my MongoDB model.

Is it possible to specify the acceptable integer values in this field?

for example, this field may only accept the integers from 1 to 10.

2

Answers


  1. You are looking for min and max validators:

    slider_value: {
      type: Number,
      required: false,
      min: 1,
      max: 10
    },
    
    Login or Signup to reply.
  2. There are min and max validators, and you should also check for isInteger

    slider_value: {
      type: Number,
      required: false,
      min: 1,
      max: 10,
      validate : {
        validator : Number.isInteger,
        message   : '{VALUE} is not an integer value'
      }
    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search