<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mitanshi Kshatriya</title>
    <description>The latest articles on DEV Community by Mitanshi Kshatriya (@mitanshikshatriya).</description>
    <link>https://dev.to/mitanshikshatriya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F499775%2Fd7cf567d-8cdf-4c8a-95b9-4bdb62869f66.png</url>
      <title>DEV Community: Mitanshi Kshatriya</title>
      <link>https://dev.to/mitanshikshatriya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mitanshikshatriya"/>
    <language>en</language>
    <item>
      <title>Enum: Restrict mongoose fields to a list of values</title>
      <dc:creator>Mitanshi Kshatriya</dc:creator>
      <pubDate>Thu, 07 Oct 2021 20:30:02 +0000</pubDate>
      <link>https://dev.to/mitanshikshatriya/enum-restrict-mongoose-fields-to-a-list-of-values-5h25</link>
      <guid>https://dev.to/mitanshikshatriya/enum-restrict-mongoose-fields-to-a-list-of-values-5h25</guid>
      <description>&lt;p&gt;Enum is an inbuilt validator in mongoose. It helps in restricting the possible values of a field to a list of values. Enums come in handy when we already know all the possible values for the field. The field can take one value out of a small set of predefined values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Imagine we need to build a schema for an online clothing website to store information about all its products. The product schema can contain fields such as the name of the product, brief description, price, quantity in stock, size, etc. A field like size can only take specific values like XS, M, XXL, etc. To enforce this we can use enum from mongoose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const productSchema = new mongoose.Schema({
    name: {
        type: String
    },
    desc:{
        type: String
    },
    price:{
        type: Number
    },
    stock:{
        type: Number
    },
    size:{
        type: String
        enum: ['XS','S','M','XL','XXL'...]
    }
}, { timestamps: true }
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this article gave you a brief idea about what, why, and how of enum.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>mongoose</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
