Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move the ‘type’ attribute into a separate file

  • Loading branch information...
commit 4a1a9b89a70d9e3a29b3148b2d175651b582207f 1 parent 8ee2dca
@Baggz authored
Showing with 64 additions and 0 deletions.
  1. +64 −0 src/engines/json/attributes/type.js
View
64 src/engines/json/attributes/type.js
@@ -0,0 +1,64 @@
+/**
+ * Type
+ * --------------------
+ */
+Validation.prototype.addAttributeConstructor('type', function typeConstructor() {
+
+ /**
+ * Types
+ */
+ var types = {
+ 'string': isString,
+ 'number': isNumber,
+ 'function': isFunction,
+ 'boolean': isBoolean,
+ 'object': isObject,
+ 'array': isArray,
+ 'integer': isInteger,
+ 'int': isInteger,
+ 'null': isNull,
+ 'any': returnTrue
+ };
+
+ // Export
+ return function type(property, propertyValue, attributeValue, propertyAttributes, callback) {
+
+ /**
+ * {
+ * type: ['string', 'number']
+ * }
+ */
+ if (isArray(attributeValue)) {
+
+ var noError = attributeValue.some(function(type) {
+ return types[type](propertyValue);
+ });
+
+ if (!noError) {
+ this.errors.addError();
+ }
+
+ return callback();
+
+ /**
+ * {
+ * type: 'string'
+ * }
+ */
+ } else {
+
+ if (!hasProperty(types, attributeValue)) {
+ throw new Error('Type ‘' + attributeValue + '’ is not supported.');
+ }
+
+ if (!types[attributeValue](propertyValue)) {
+ this.addError();
+ }
+
+ return callback();
+
+ }
+
+ };
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.