Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose schema required field not working for multiple values

I'm creating a mongoose schema and want to minimize the code. I want size to be required for the fields shown. But when I test it I don't get an error if size isn't included. I tried: (('flag-silk' || 'costume') || 'accessories-shoes'), ['flag-silk' || 'costume' || 'accessories-shoes'], and ==. Am I missing something or is my syntax wrong?

code:

size: {
      type: String,
      required: [
        function() {
          return this.category === ('flag-silk' || 'costume' || 'accessories-shoes');
        },
        'Please enter the size of the product.'
      ]
    },
like image 259
Simca Avatar asked Nov 23 '25 04:11

Simca


1 Answers

That's not the right syntax for the boolean expression you need. Instead, use:

type: String,
required: [
  function() {
      return ['flag-silk', 'costume', 'accessories-shoes'].indexOf(this.category) !== -1;
    },
    'Please enter the size of the product.'
  ]
},
like image 142
JohnnyHK Avatar answered Nov 25 '25 22:11

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!