Permalink
Browse files

Use the ‘addAttributeConstructor’ method only when necessary

  • Loading branch information...
1 parent 36dbb63 commit 83f0dd13491c16e24cfd142ceb21dabf52d89d63 @Baggz committed Mar 12, 2012
@@ -1,14 +1,12 @@
/**
* DivisibleBy
*/
-Validation.prototype.addAttributeConstructor('divisibleBy', function divisibleByConstructor() {
- return function divisibleBy(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var divisibleByAttribute = function divisibleBy(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isNumber(propertyValue) && (propertyValue % attributeValue !== 0)) {
+ this.addError();
+ }
+ return callback();
+};
- if (isNumber(propertyValue) && (propertyValue % attributeValue !== 0)) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('divisibleBy', divisibleByAttribute);
@@ -1,14 +1,12 @@
/**
* Enum
*/
-Validation.prototype.addAttributeConstructor('enum', function enumConstructor() {
- return function(property, propertyValue, attributeValue, propertyAttributes, callback) {
-
- if (attributeValue.indexOf(propertyValue) === -1) {
- this.addError();
- }
-
- return callback();
+var enumAttribute = function(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (attributeValue.indexOf(propertyValue) === -1) {
+ this.addError();
+ }
+ return callback();
+};
- };
-});
+// Export
+Validation.prototype.addAttribute('enum', enumAttribute);
@@ -1,14 +1,12 @@
/**
* Except
*/
-Validation.prototype.addAttributeConstructor('except', function exceptConstructor() {
- return function except(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var exceptAttribute = function except(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (attributeValue.indexOf(propertyValue) !== -1) {
+ this.addError('except', property);
+ }
+ return callback();
+};
- if (attributeValue.indexOf(propertyValue) !== -1) {
- this.addError('except', property);
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('except', exceptAttribute);
@@ -1,14 +1,12 @@
/**
* Length
*/
-Validation.prototype.addAttributeConstructor('length', function lengthConstructor() {
- return function length(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var lengthAttribute = function length(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isString(propertyValue) && propertyValue.length !== attributeValue) {
+ this.addError();
+ }
+ return callback();
+};
- if (isString(propertyValue) && propertyValue.length !== attributeValue) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('length', lengthAttribute);
@@ -1,14 +1,12 @@
/**
* MaxItems
*/
-Validation.prototype.addAttributeConstructor('maxItems', function maxItemsConstructor() {
- return function maxItems(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var maxItemsAttribute = function maxItems(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isArray(propertyValue) && propertyValue.length > attributeValue) {
+ this.addError();
+ }
+ return callback();
+};
- if (isArray(propertyValue) && propertyValue.length > attributeValue) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('maxItems', maxItemsAttribute);
@@ -1,14 +1,12 @@
/**
* MaxLength
*/
-Validation.prototype.addAttributeConstructor('maxLength', function maxLengthConstructor() {
- return function maxLength(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var maxLengthAttribute = function maxLength(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isString(propertyValue) && propertyValue.length > attributeValue) {
+ this.addError();
+ }
+ return callback();
+};
- if (isString(propertyValue) && propertyValue.length > attributeValue) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('maxLength', maxLengthAttribute);
@@ -1,16 +1,17 @@
/**
* Maximum
*/
-Validation.prototype.addAttributeConstructor('required', function maximumConstructor() {
- return function maximum(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var maximumAttribute = function maximum(property, propertyValue, attributeValue, propertyAttributes, callback) {
- if (isNumber(propertyValue)) {
- if ((propertyAttributes.exclusiveMaximum && propertyValue >= attributeValue) || (propertyValue > attributeValue)) {
- this.addError();
- }
+ if (isNumber(propertyValue)) {
+ if ((propertyAttributes.exclusiveMaximum && propertyValue >= attributeValue) || (propertyValue > attributeValue)) {
+ this.addError();
}
-
- return callback();
+ }
- };
-});
+ return callback();
+
+};
+
+// Export
+Validation.prototype.addAttribute('maximum', maximumAttribute);
@@ -1,14 +1,12 @@
/**
* MinItems
*/
-Validation.prototype.addAttributeConstructor('minItems', function minItemsConstructor() {
- return function minItems(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var minItems = function minItems(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isArray(propertyValue) && propertyValue.length < attributeValue) {
+ this.addError();
+ }
+ return callback();
+};
- if (isArray(propertyValue) && propertyValue.length < attributeValue) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('minItems', minItems);
@@ -1,14 +1,12 @@
/**
* MinLength
*/
-Validation.prototype.addAttributeConstructor('minLength', function minLengthConstructor() {
- return function minLength(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var minLengthAttribute = function minLength(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isString(propertyValue) && propertyValue.length < attributeValue) {
+ this.addError();
+ }
+ return callback();
+};
- if (isString(propertyValue) && propertyValue.length < attributeValue) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('minLength', minLengthAttribute);
@@ -1,16 +1,14 @@
/**
* Minimum
*/
-Validation.prototype.addAttributeConstructor('minimum', function minimumConstructor() {
- return function minimum(property, propertyValue, attributeValue, propertyAttributes, callback) {
-
- if (isNumber(propertyValue)) {
- if ((propertyAttributes.exclusiveMinimum && propertyValue <= attributeValue) || (propertyValue < attributeValue)) {
- this.addError();
- }
+var minimumAttribute = function minimum(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isNumber(propertyValue)) {
+ if ((propertyAttributes.exclusiveMinimum && propertyValue <= attributeValue) || (propertyValue < attributeValue)) {
+ this.addError();
}
+ }
+ return callback();
+};
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('minimum', minimumAttribute);
@@ -1,14 +1,12 @@
/**
* Pattern
*/
-Validation.prototype.addAttributeConstructor('pattern', function patternConstructor() {
- return function pattern(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var patternAttribute = function pattern(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (isString(propertyValue) && !propertyValue.match(attributeValue)) {
+ this.addError();
+ }
+ return callback();
+};
- if (isString(propertyValue) && !propertyValue.match(attributeValue)) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('pattern', patternAttribute);
@@ -1,15 +1,12 @@
/**
* Required
- * --------------------
*/
-Validation.prototype.addAttributeConstructor('required', function requiredConstructor() {
- return function required(property, propertyValue, attributeValue, propertyAttributes, callback) {
+var requiredAttribute = function required(property, propertyValue, attributeValue, propertyAttributes, callback) {
+ if (attributeValue && isUndefined(propertyValue)) {
+ this.addError();
+ }
+ return callback();
+};
- if (attributeValue && isUndefined(propertyValue)) {
- this.addError();
- }
-
- return callback();
-
- };
-});
+// Export
+Validation.prototype.addAttribute('required', requiredAttribute);
@@ -0,0 +1,19 @@
+/**
+ * UniqueItems
+ */
+var uniqueItemsAttribute = function uniqueItems(property, propertyValue, attributeValue, propertyAttributes, callback) {
+
+ var self = this;
+
+ each(propertyValue, function(index, value) {
+ if ((propertyValue.indexOf(value) < index)) {
+ self.addError();
+ }
+ });
+
+ return callback();
+
+};
+
+// Export
+Validation.prototype.addAttribute('uniqueItems', uniqueItemsAttribute);

0 comments on commit 83f0dd1

Please sign in to comment.