Skip to content
This repository

Required Objects #29

Closed
wants to merge 6 commits into from

3 participants

Barret Schloerke David Ihnen František Hába
Barret Schloerke

These changes should allow objects to fit a certain mold while being flexible.

Example Schema:

var schema = {
  type: 'object',
  properties: {
    user: {
      type: 'object',
      properties: {
        name: {
          required: true
        },
        surname: {
          required: true
        } } } } };

It doesn't require anything, but if it is there, it should be an object.
If it has a field of 'user', it should be an object.
If field 'user' exists, then 'name' and 'surname' are required to be present.

Currently, the 'user' field is not taken into account when looking at it's required subfields: 'name' and 'surname'. If 'user' is not present and not required, then I believe there is no need to check for it's sub fields.

Best,
Barret

David Ihnen

This is totally needed. You can't be requiring the contents of optional schema bits.

František Hába Baggz closed this December 26, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
53  src/amanda.js
@@ -24,7 +24,7 @@
24 24
 
25 25
   /**
26 26
    * IsEmpty
27  
-   *  
  27
+   *
28 28
    * Returns true if the passed-in object is empty.
29 29
    *
30 30
    * @param {object} input
@@ -96,7 +96,7 @@
96 96
           if (list.hasOwnProperty(key)) {
97 97
             iterator.apply(list, [key, list[key]]);
98 98
           }
99  
-        } 
  99
+        }
100 100
       }
101 101
 
102 102
     };
@@ -216,7 +216,7 @@
216 216
   }, function(key, value) {
217 217
     Error.prototype[key] = function() {
218 218
       return pluck(this, value);
219  
-    };    
  219
+    };
220 220
   });
221 221
 
222 222
   /**
@@ -275,10 +275,10 @@
275 275
    * @param {object} templateData
276 276
    */
277 277
   Validation.prototype.renderErrorMessage = function(validatorName, templateData) {
278  
-    
  278
+
279 279
     // Gets an error message
280 280
     var errorMessage = this.messages[validatorName];
281  
-    
  281
+
282 282
     // If the error message is a function
283 283
     if (typeof errorMessage === 'function') {
284 284
       return errorMessage(
@@ -403,7 +403,7 @@
403 403
    * @param {function} callback
404 404
    */
405 405
   Validation.prototype.validateProperties = function(instance, schema, path, callback) {
406  
-    
  406
+
407 407
     // Save a reference to the ‘this’
408 408
     var self = this;
409 409
 
@@ -433,20 +433,27 @@
433 433
        *   }
434 434
        * }
435 435
        */
436  
-      if (isObject || isArray)  {
437  
-        return self.validateSchema(
438  
-          propertyValue,
439  
-          schema.properties[property],
440  
-          propertyPath,
441  
-          callback
442  
-        );
  436
+      if (instance || property == "required") {
  437
+        // only do work if the instance itself exists
  438
+        if (isObject || isArray)  {
  439
+          return self.validateSchema(
  440
+            propertyValue,
  441
+            schema.properties[property],
  442
+            propertyPath,
  443
+            callback
  444
+          );
  445
+        } else {
  446
+          return self.validateProperty(
  447
+            propertyPath,
  448
+            propertyValue,
  449
+            propertyValidators,
  450
+            callback
  451
+          );
  452
+        }
  453
+
443 454
       } else {
444  
-        return self.validateProperty(
445  
-          propertyPath,
446  
-          propertyValue,
447  
-          propertyValidators,
448  
-          callback
449  
-        );
  455
+        // not required... carry on
  456
+        callback();
450 457
       }
451 458
 
452 459
     }, callback);
@@ -544,7 +551,7 @@
544 551
      * {
545 552
      *   type: 'object',
546 553
      *   properties: {
547  
-     *     ... 
  554
+     *     ...
548 555
      *   }
549 556
      * }
550 557
      * — or —
@@ -561,7 +568,7 @@
561 568
          * {
562 569
          *   type: 'object',
563 570
          *   properties: {
564  
-         *     ... 
  571
+         *     ...
565 572
          *   }
566 573
          * }
567 574
          */
@@ -573,7 +580,7 @@
573 580
          *   type: 'array',
574 581
          *   items: {
575 582
          *     type: 'string'
576  
-         *     ... 
  583
+         *     ...
577 584
          *   }
578 585
          * }
579 586
          */
@@ -637,7 +644,7 @@
637 644
      * Type
638 645
      */
639 646
     'type': (function() {
640  
-      
  647
+
641 648
       var types = {
642 649
         'object': function(input) {
643 650
           return Object.prototype.toString.call(input) === '[object Object]';
98  tests/validators/required/objects.js
@@ -31,7 +31,7 @@ exports['Test #1'] = function(test) {
31 31
   };
32 32
 
33 33
   amanda.validate(data, schema, { singleError: false }, function(error) {
34  
-    
  34
+
35 35
     test.equal(error, undefined);
36 36
 
37 37
   });
@@ -42,37 +42,6 @@ exports['Test #1'] = function(test) {
42 42
 
43 43
 exports['Test #2'] = function(test) {
44 44
 
45  
-  var data = {};
46  
-
47  
-  amanda.validate(data, schema, { singleError: false }, function(error) {
48  
-
49  
-    delete error[0].message;
50  
-    delete error[1].message;
51  
-
52  
-    test.deepEqual(error[0], {
53  
-      property: 'user.name',
54  
-      propertyValue: undefined,
55  
-      validator: 'required',
56  
-      validatorValue: true
57  
-    });
58  
-
59  
-    test.deepEqual(error[1], {
60  
-      property: 'user.surname',
61  
-      propertyValue: undefined,
62  
-      validator: 'required',
63  
-      validatorValue: true
64  
-    });
65  
-
66  
-    test.equal(error.length, 2);
67  
-
68  
-  });
69  
-
70  
-  test.done();
71  
-
72  
-};
73  
-
74  
-exports['Test #3'] = function(test) {
75  
-
76 45
   var data = {
77 46
     user: 123
78 47
   };
@@ -112,14 +81,14 @@ exports['Test #3'] = function(test) {
112 81
 
113 82
 };
114 83
 
115  
-exports['Test #4'] = function(test) {
  84
+exports['Test #3'] = function(test) {
116 85
 
117 86
   var data = {
118 87
     user: {}
119 88
   };
120 89
 
121 90
   amanda.validate(data, schema, { singleError: false }, function(error) {
122  
-    
  91
+
123 92
     delete error[0].message;
124 93
     delete error[1].message;
125 94
 
@@ -146,7 +115,7 @@ exports['Test #4'] = function(test) {
146 115
 
147 116
 };
148 117
 
149  
-exports['Test #5'] = function(test) {
  118
+exports['Test #4'] = function(test) {
150 119
 
151 120
   var data = {
152 121
     user: {
@@ -155,7 +124,7 @@ exports['Test #5'] = function(test) {
155 124
   };
156 125
 
157 126
   amanda.validate(data, schema, { singleError: false }, function(error) {
158  
-    
  127
+
159 128
     delete error[0].message;
160 129
 
161 130
     test.deepEqual(error[0], {
@@ -173,7 +142,7 @@ exports['Test #5'] = function(test) {
173 142
 
174 143
 };
175 144
 
176  
-exports['Test #6'] = function(test) {
  145
+exports['Test #5'] = function(test) {
177 146
 
178 147
   var data = {
179 148
     user: {
@@ -182,7 +151,7 @@ exports['Test #6'] = function(test) {
182 151
   };
183 152
 
184 153
   amanda.validate(data, schema, { singleError: false }, function(error) {
185  
-    
  154
+
186 155
     delete error[0].message;
187 156
 
188 157
     test.deepEqual(error[0], {
@@ -200,57 +169,22 @@ exports['Test #6'] = function(test) {
200 169
 
201 170
 };
202 171
 
  172
+
203 173
 /**
204  
- * Test #7
  174
+ * Test #6
  175
+ *
  176
+ * Allow object to be empty as the user value is not required
205 177
  */
206  
-exports['Test #7'] = function(test) {
207  
-
208  
-  var schema = {
209  
-    type: 'object',
210  
-    properties: {
211  
-      information: {
212  
-        type: 'object',
213  
-        properties: {
214  
-          user: {
215  
-            type: 'object',
216  
-            properties: {
217  
-              name: {
218  
-                required: true
219  
-              },
220  
-              surname: {
221  
-                required: true,
222  
-              }
223  
-            }
224  
-          }
225  
-        }
226  
-      }
227  
-    }
228  
-  };
229  
-
  178
+exports['Test #6'] = function(test) {
230 179
 
231  
-  amanda.validate({}, schema, { singleError: false }, function(error) {
232  
-    
233  
-    delete error[0].message;
234  
-    delete error[1].message;
  180
+  var data = {};
235 181
 
236  
-    test.deepEqual(error[0], {
237  
-      property: 'information.user.name',
238  
-      propertyValue: undefined,
239  
-      validator: 'required',
240  
-      validatorValue: true
241  
-    });
242  
-  
243  
-    test.deepEqual(error[1], {
244  
-      property: 'information.user.surname',
245  
-      propertyValue: undefined,
246  
-      validator: 'required',
247  
-      validatorValue: true
248  
-    });
  182
+  amanda.validate(data, schema, { singleError: false }, function(error) {
249 183
 
250  
-    test.equal(error.length, 2);
  184
+    test.equal(error, undefined);
251 185
 
252 186
   });
253 187
 
254 188
   test.done();
255 189
 
256  
-};
  190
+};
58  tests/validators/required/objectsSingleError.js
@@ -31,7 +31,7 @@ exports['Test #1'] = function(test) {
31 31
   };
32 32
 
33 33
   amanda.validate(data, schema, function(error) {
34  
-    
  34
+
35 35
     test.equal(error, undefined);
36 36
 
37 37
   });
@@ -42,29 +42,6 @@ exports['Test #1'] = function(test) {
42 42
 
43 43
 exports['Test #2'] = function(test) {
44 44
 
45  
-  var data = {};
46  
-
47  
-  amanda.validate(data, schema, function(error) {
48  
-
49  
-    delete error[0].message;
50  
-
51  
-    test.deepEqual(error[0], {
52  
-      property: 'user.name',
53  
-      propertyValue: undefined,
54  
-      validator: 'required',
55  
-      validatorValue: true
56  
-    });
57  
-
58  
-    test.equal(error.length, 1);
59  
-
60  
-  });
61  
-
62  
-  test.done();
63  
-
64  
-};
65  
-
66  
-exports['Test #3'] = function(test) {
67  
-
68 45
   var data = {
69 46
     user: 123
70 47
   };
@@ -96,14 +73,14 @@ exports['Test #3'] = function(test) {
96 73
 
97 74
 };
98 75
 
99  
-exports['Test #4'] = function(test) {
  76
+exports['Test #3'] = function(test) {
100 77
 
101 78
   var data = {
102 79
     user: {}
103 80
   };
104 81
 
105 82
   amanda.validate(data, schema, function(error) {
106  
-    
  83
+
107 84
     delete error[0].message;
108 85
 
109 86
     test.deepEqual(error[0], {
@@ -121,7 +98,7 @@ exports['Test #4'] = function(test) {
121 98
 
122 99
 };
123 100
 
124  
-exports['Test #5'] = function(test) {
  101
+exports['Test #4'] = function(test) {
125 102
 
126 103
   var data = {
127 104
     user: {
@@ -130,7 +107,7 @@ exports['Test #5'] = function(test) {
130 107
   };
131 108
 
132 109
   amanda.validate(data, schema, function(error) {
133  
-    
  110
+
134 111
     delete error[0].message;
135 112
 
136 113
     test.deepEqual(error[0], {
@@ -148,7 +125,7 @@ exports['Test #5'] = function(test) {
148 125
 
149 126
 };
150 127
 
151  
-exports['Test #6'] = function(test) {
  128
+exports['Test #5'] = function(test) {
152 129
 
153 130
   var data = {
154 131
     user: {
@@ -157,7 +134,7 @@ exports['Test #6'] = function(test) {
157 134
   };
158 135
 
159 136
   amanda.validate(data, schema, function(error) {
160  
-    
  137
+
161 138
     delete error[0].message;
162 139
 
163 140
     test.deepEqual(error[0], {
@@ -173,4 +150,23 @@ exports['Test #6'] = function(test) {
173 150
 
174 151
   test.done();
175 152
 
176  
-};
  153
+};
  154
+
  155
+/**
  156
+ * Test #6
  157
+ *
  158
+ * Allow object to be empty as the user value is not required
  159
+ */
  160
+exports['Test #6'] = function(test) {
  161
+
  162
+  var data = {};
  163
+
  164
+  amanda.validate(data, schema, function(error) {
  165
+
  166
+    test.equal(error, undefined);
  167
+
  168
+  });
  169
+
  170
+  test.done();
  171
+
  172
+};
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.