{"id":11853,"date":"2014-02-19T21:42:28","date_gmt":"2014-02-19T16:12:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11853"},"modified":"2016-12-19T15:31:07","modified_gmt":"2016-12-19T10:01:07","slug":"validation-with-mongoose","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/validation-with-mongoose\/","title":{"rendered":"Validation with Mongoose"},"content":{"rendered":"<p>In one of our <strong>Node.js<\/strong> projects, we used <strong>Mongoose<\/strong> module to interact with <strong>MongoDB<\/strong>. <strong>Mongoose<\/strong> provides us four types of built-in validation on <strong>schema<\/strong> as below:<\/p>\n<p><strong>1. Required<\/strong>: We can mark a field as <strong>required<\/strong>, which must be provided.<br \/>\n<strong>2. Limit<\/strong>: If field is type of <strong>Number<\/strong> in <strong>Schema<\/strong>, then we can restrict <strong>maximum<\/strong> and <strong>minimum<\/strong> value for that field.<br \/>\n<strong>3. ENUM<\/strong>: If field is type of <strong>String<\/strong> in <strong>Schema<\/strong>, then we can provide the list of <strong>Strings<\/strong> which should <strong>match<\/strong> with the value in that field.<br \/>\n<strong>4. Match<\/strong>: If field is type of <strong>String<\/strong> in <strong>Schema<\/strong>, then we can provide <strong>regex<\/strong> to <strong>match<\/strong> that value.<\/p>\n<p>Let\u2019s go through each one of them with examples:<\/p>\n<p>[js]<br \/>\n\/*<br \/>\n* requiring mongoose module.<br \/>\n* *\/<br \/>\nvar mongoose = require (&#8216;mongoose&#8217;);<\/p>\n<p>\/\/ Defining ENUMs for the gender field which will use for validation.<br \/>\nvar genders = &#8216;MALE FEMALE&#8217;.split(&#8216; &#8216;)<\/p>\n<p>\/\/ Connecting with mongodb(validation db).<br \/>\nmongoose.connect(&#8216;mongodb:\/\/localhost\/validation&#8217;);<\/p>\n<p>\/*<br \/>\n* Defining User Schema with name(as Required Field), age(with Minimum and Maximum values),<br \/>\n* gender (which is ENUM, so it can be MALE or FEMALE only) and last one is email (which<br \/>\n* will match the provided regex.)<br \/>\n* *\/<br \/>\nvar UserSchema = mongoose.Schema({<br \/>\n    name: {type: String, required: true}, \/\/ 1. Required validation<br \/>\n    age: {type: Number, min: 16, max: 60}, \/\/ 2. Minimum and Maximum value validation<br \/>\n    gender: {type: String, enum: genders}, \/\/ 3. ENUM validation<br \/>\n    email: {type: String, match: \/\\S+@\\S+\\.\\S+\/} \/\/ 4. Match validation via regex<br \/>\n});<\/p>\n<p>var User = mongoose.model(&#8216;User&#8217;, UserSchema);<\/p>\n<p>\/*<br \/>\n* When we call save function on the model, then first it calls validate function to validate<br \/>\n* the values. Validate method takes callback as argument, if any error occurs at the time<br \/>\n* of validating values then validate function calls callback with errors otherwise calls<br \/>\n* with null.<br \/>\n* *\/<br \/>\nnew User({age: 25}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error for Name Field because its marked as Required.<br \/>\n});<br \/>\nnew User({name: &quot;Amit Thakkar&quot;, age:15}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error for Age Field, age is less than Minimum value.<br \/>\n});<br \/>\nnew User({name: &quot;Amit Thakkar&quot;, age:61}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error for Age Field, age is more than Maximum value.<br \/>\n});<br \/>\nnew User({name: &quot;Amit Thakkar&quot;, age:25, gender:&quot;male&quot;}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error for Gender Field, male does not match with ENUM.<br \/>\n});<br \/>\nnew User({name: &quot;Amit Thakkar&quot;, age:25, gender:&quot;MALE&quot;, email:&quot;amit.kumar@intelligrape&quot;}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error for Invalid Email id.<br \/>\n});<br \/>\nnew User({name: &quot;Amit Thakkar&quot;, age:25, gender:&quot;MALE&quot;, email:&quot;amit.kumar@intelligrape.com&quot;}).validate(function (error) {<br \/>\n    console.log(&quot;ERROR: &quot;, error); \/\/ Error will be undefined<br \/>\n});<br \/>\n[\/js]<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of our Node.js projects, we used Mongoose module to interact with MongoDB. Mongoose provides us four types of built-in validation on schema as below: 1. Required: We can mark a field as required, which must be provided. 2. Limit: If field is type of Number in Schema, then we can restrict maximum and [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[1185],"tags":[1317,1318,4846,1316,1124,1177,901],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11853"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11853"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11853\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}