Asaf Baibekov
Asaf Baibekov

Reputation: 375

Mongoose - array of enum strings

I have a Schema that has a property with the type of array of strings that are predefined.

This is what I've tried to do:

interests: {
    type: [String],
    enum: ['football', 'basketball', 'read'],
    required: true
}

The thing is that when I'm trying to enter a wrong value that isn't defined on the enum, to the array it wouldn't validate it with the enum list.

for example, this would pass which it shouldn't:

{ "interests": ["football", "asdf"] }

because "asdf" isn't predefined in the enum list it shouldn't pass the validation but unfortunately, it passes the validation and saves it.

I've tried to check this thing with a string type of values instead of an array of strings and it works.

for example:

interests: {
    type: String,
    enum: ['football', 'basketball', 'read'],
    required: true
}

for example, this is failing as expected:

{ "interest": "asdf" }

In conclusion, I need a schema's property with a type of array of strings that would check it's elements based on predefined values

Is the most effective way to achieve this goal is by using the validate method or there is a better way?

Upvotes: 8

Views: 15758

Answers (7)

Robin D.
Robin D.

Reputation: 56

This works for me with validation correctly applied:

const schema = new Schema({
  interests: {
    type: [String],
    enum: ['football', 'basketball', 'read']
  }
}, { timestamps: true })

Upvotes: 1

Omar Bahutair
Omar Bahutair

Reputation: 87

You can try this

const InterestSchema = new Schema({
 interests: {
   type: [{
     enum: ['which', 'ever', 'values', 'allowed'],
   ]}
  }
})

hope this helps

Upvotes: 0

Garry
Garry

Reputation: 41

with nestjs without to create subschema solution


export enum RolesEnum {
  User = 'user',
  Admin = 'admin'
}

...
export class User {
  ...
  @Prop({
    type: [String],
    enum: [RolesEnum.Admin, RolesEnum.User],
    default: []
  })
  roles: RolesEnum[]
  ...
}
...

Upvotes: 2

Iddan Aaronsohn
Iddan Aaronsohn

Reputation: 1210

Quoting from here:

const SubStrSz = new mongoose.Schema({ value: { type: String, enum: ['qwerty', 'asdf'] } });
const MySchema = new mongoose.Schema({ array: [SubStrSz] });

Using that technique you will able to validate values inside of your array.

Upvotes: 6

Shehan Hasintha
Shehan Hasintha

Reputation: 1165

use ref to make a relation to the defined enum schema

const actionsEnums = new Mongoose.Schema({value: { type: String, enum:["account-deletion","account-update"]}});

const Privilege = new Mongoose.Schema(
  {
    userLevel: { type: String, required: true },
    actions: [{type: String, refs: actionsEnums}],
})

Upvotes: 0

Umer Baba
Umer Baba

Reputation: 351

Here distributers will be array of distributerObj, similarly you can define object of any type.

const distributerObj = new Schema({
    "dis_id": {
        "type": "String"
    },
    "status": {
        "type": "String"
    }
});
const productSchema = new Schema({
    "distributers": {
        "type": [distributerObj]
    }
});

Upvotes: 0

yanai edri
yanai edri

Reputation: 369

You can try a custom validation?Like this

const userSchema = new Schema({
  phone: {
    type: String,
    validate: {
      validator: function(v) {
        return /\d{3}-\d{3}-\d{4}/.test(v);
      },
      message: props => `${props.value} is not a valid phone number!`
    },
    required: [true, 'User phone number required']
  }
});

this is the docs: https://mongoosejs.com/docs/validation.html

Upvotes: 2

Related Questions