Repository: washingtonpost/ans-schema
Pull request #72 (2016-07-13 20:22:04)

Check underscore schemas for types with hyphens


Patch:
diff --git a/lib/validator.js b/lib/validator.js
index dbf8f5e..514f220 100644
--- a/lib/validator.js
+++ b/lib/validator.js
@@ -9,7 +9,8 @@ var UTILS = '/utils';
 var TRAITS = '/traits';
 
 // Top-level content types
-var TYPES = [ 'story', 'image', 'video', 'gallery', 'results', 'content' ];
+//var TYPES = [ 'story', 'image', 'video', 'gallery', 'results', 'content' ];
+var TYPES = [];
 
 var VERSIONS = _.map(
   _.filter(version.history, function (item) {
@@ -27,9 +28,17 @@ var AnsValidator = function(_ajv, _schemas) {
   this.schemas = {};
   var _this = this;
 
-
   _.forEach(VERSIONS, function(version) {
     _this.schemas[version] = {};
+
+    TYPES = _.filter(_.map(_.filter(_.keys(_schemas), function(item) {
+      return item.indexOf(version + "/") === 0;
+    }), function(item) {
+      return item.substring(version.length + 1, item.indexOf(".json"))
+    }), function(item) {
+      return item.indexOf("/") === -1;
+    });
+
     _.forEach(TYPES, function(type) {
       if (_.has(_schemas, version + '/' + type + ".json")) {
         var content = _schemas[version + '/' + type + ".json"];
@@ -100,14 +109,23 @@ AnsValidator.prototype.findSchema = function findSchema(version, name) {
     if (_.has(this.schemas[version], name)) {
       return this.schemas[version][name];
     }
+    else {
+      // Workaround for schemas whose type was determined before the "no hyphens" rule
+      var alt_name = name.replace("-", "_");
+      if (_.has(this.schemas[version], alt_name)) {
+        return this.schemas[version][alt_name];
+      }
+    }
   }
   return null;
 };
 
+
 AnsValidator.prototype.validate = function validate(ans) {
   return this.getAllContentErrors(ans, ans.version, "");
 };
 
+
 // Recursively validate an object and all relevant children
 // against their respective element schemas.
 AnsValidator.prototype.getAllContentErrors = function getAllContentErrors(ans, version, namespace) {


Test patch:
diff --git a/tests/validator.js b/tests/validator.js
index 1fe9d81..58413d4 100644
--- a/tests/validator.js
+++ b/tests/validator.js
@@ -177,6 +177,43 @@ describe("ANS Validator", function() {
     });
 
 
+    it("should validate items with a hyphen in their type", function() {
+      var image_operation = {
+        "_id": "1",
+        "body": {
+          "_id": "2",
+          "additional_properties": {         "alttext": "REUTERS"       },
+          "caption": "Indiana Pacers David West celebrates during Game 6 of their NBA Eastern Conference Final playoff series against the Miami Heat in Indianapolis, Indiana June 1, 2013.",
+          "credits": {
+            "photographer": [
+              {
+                "name": "BRENT SMITH",
+                "type": "author"
+              }
+            ]
+          },
+          "owner": {
+            "name": "REUTERS"
+          },
+          "revision": {
+            "revision_id": ""
+          },
+          "type": "image",
+          "url": "http://www.theglobeandmail.com/sports/basketball/article12301697.ece/BINARY/IND164_NBA.JPG",
+          "version": "0.5.6"
+        },
+        "date": "2013-06-02T03:18:49.053Z",
+        "operation": "update",
+        "organization_id": "sandbox.tgam",
+        "type": "image-operation",
+        "version": "0.5.6"
+      };
+      var errors = validator.getAllContentErrors(image_operation, version, "");
+      errors.length.should.eql(0);
+
+    });
+
+
   });