Skip to content
This repository
Browse code

Externalized schema definitions

  • Loading branch information...
commit e522df2ab1cb92201aa2ed7096dca553ef25e2f7 1 parent b2c7715
Klemen Slavič authored August 30, 2011
1  example.html
@@ -5,6 +5,7 @@
5 5
         <title>HTML5 microdata test page</title>
6 6
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
7 7
         <script src="jquery.microdata.js"></script>
  8
+        <script src="schemas.js"></script>
8 9
     </head>
9 10
     <body>
10 11
         <h1>HTML5 microdata test page</h1>
84  jquery.microdata.js
@@ -46,76 +46,8 @@
46 46
         },
47 47
         
48 48
         
49  
-        // would love it if I could externalize this via cached web service
50  
-        validationRules = {
51  
-            "http://data-vocabulary.org/Event" :
52  
-                [
53  
-                     { name: "summary",     required: true,  type: "text",     validator: validators.text     },
54  
-                     { name: "url",         required: false, type: "url",      validator: validators.url      },
55  
-                     { name: "location",    required: false, type: "complex",  validator: validators.complex  }, // optionally represented by data-vocabulary.org/Organization or data-vocabulary.org/Address
56  
-                     { name: "description", required: false, type: "text",     validator: validators.text     },
57  
-                     { name: "startdate",   required: true,  type: "datetime", validator: validators.datetime },
58  
-                     { name: "enddate",     required: false, type: "datetime", validator: validators.datetime },
59  
-                     { name: "duration",    required: false, type: "duration", validator: validators.duration },
60  
-                     { name: "eventtype",   required: false, type: "text",     validator: validators.text     },
61  
-                     { name: "geo",         required: false, type: "complex",  validator: validators.complex  }, // represented by itemscope with two properties: latitude and longitude
62  
-                     { name: "photo",       required: false, type: "url",      validator: validators.url      } 
63  
-                ],
64  
-                
65  
-            "http://data-vocabulary.org/Person" :
66  
-                [
67  
-                     { name: "name",         required: false, type: "text",    validator: validators.text    },
68  
-                     { name: "fn",           required: false, type: "text",    validator: validators.text    }, // alias for "name"
69  
-                     { name: "nickname",     required: false, type: "text",    validator: validators.text    },
70  
-                     { name: "photo",        required: false, type: "url",     validator: validators.url     },
71  
-                     { name: "title",        required: false, type: "text",    validator: validators.text    },
72  
-                     { name: "role",         required: false, type: "text",    validator: validators.text    },
73  
-                     { name: "url",          required: false, type: "url",     validator: validators.url     },
74  
-                     { name: "affiliation",  required: false, type: "text",    validator: validators.text    },
75  
-                     { name: "org",          required: false, type: "text",    validator: validators.text    }, // alias for "affiliation"
76  
-                     { name: "address",      required: false, type: "complex", validator: validators.complex }, // can have subproperties street-address, city, region, postal-code, country-name
77  
-                     { name: "adr",          required: false, type: "complex", validator: validators.complex }, // alias for "address"
78  
-                     { name: "friend",       required: false, type: "url",     validator: validators.url     },
79  
-                     { name: "contact",      required: false, type: "url",     validator: validators.url     },
80  
-                     { name: "acquaintance", required: false, type: "url",     validator: validators.url     }
81  
-                     // NOTE: to define "friend", "contact" or "acquaintance", you can also use XFN rel="..."
82  
-                ],
83  
-            
84  
-            "http://data-vocabulary.org/Organization" :
85  
-                [
86  
-                     { name: "name",    required: false, type: "text",    validator: validators.text    },
87  
-                     { name: "fn",      required: false, type: "text",    validator: validators.text    }, // alias for "name"
88  
-                     { name: "org",     required: false, type: "text",    validator: validators.text    }, // alias for "name"
89  
-                     { name: "url",     required: false, type: "url",     validator: validators.url     },
90  
-                     { name: "address", required: false, type: "complex", validator: validators.complex },
91  
-                     { name: "adr",     required: false, type: "complex", validator: validators.complex }, // alias for "address"
92  
-                     { name: "tel",     required: false, type: "text",    validator: validators.text    },
93  
-                     { name: "geo",     required: false, type: "complex", validator: validators.complex }
94  
-                ],
95  
-                
96  
-            "http://data-vocabulary.org/Offer" :
97  
-                [
98  
-                     { name: "price",           required: false, type: "float",    validator: validators.float    },
99  
-                     { name: "currency",        required: false, type: "text",     validator: validators.currency },
100  
-                     { name: "pricevaliduntil", required: false, type: "datetime", validator: validators.datetime },
101  
-                     { name: "seller",          required: false, type: "complex",  validator: validators.complex  }, // can contain a Person or Organization
102  
-                     {
103  
-                         name: "condition",     required: false, type: "complex",
104  
-                         validator: function(value, el) {
105  
-                             return $(el).hasAttr('content') && $.inArray($(el).attr('content').toLowerCase(), ["new", "used", "refurbished"]);
106  
-                         }
107  
-                     }, 
108  
-                     {
109  
-                         name: "availability",  required: false, type: "complex",
110  
-                         validator: function(value, el) { 
111  
-                             return $(el).hasAttr('content') && $.inArray($(el).attr('content').toLowerCase(), ["out_of_stock", "in_stock", "instore_only", "preorder"]);
112  
-                         }
113  
-                     },
114  
-                     { name: "offerurl",        required: false, type: "url",      validator: validators.url      }, // points to a product web page that includes the offer
115  
-                     { name: "identifier",      required: false, type: "text",     validator: validators.text     }, // recognizes ASIN, ISBN, MPN, UPC, SKU; suggests including product prand and at least one of the identifiers
116  
-                     { name: "itemoffered",     required: false, type: "complex",  validator: validators.complex  }  // can contain free text, a Product or other item types
117  
-                ]
118  
-        };
  49
+        // this is filled up using schemas.js
  50
+        validationRules = {};
119 51
     
120 52
     
121 53
     /**
@@ -226,11 +158,7 @@
226 158
         var type = mdata.type,
227 159
             t = $('<li title="' + type + '">' + (validators.url(type)? '<a href="' + type + '">' + type.replace(/^.*\//, '') + '</a>': "[no vocabulary]") + '</li>').appendTo(widget),
228 160
             u = $('<ul/>').appendTo(t),
229  
-            rules = [],
230  
-            required = [],
231  
-            rule,
232 161
             prop,
233  
-            validationExists = false,
234 162
             i;
235 163
         
236 164
         mdata = (typeof mdata.valid === 'undefined')? mdata = validateData(mdata): mdata;
@@ -279,6 +207,10 @@
279 207
         items.each(function() { addObject(this, parseElement(this)); });
280 208
     };
281 209
     
  210
+    var addDefinition = function(url, fields) {
  211
+        validationRules[url] = fields;
  212
+    };
  213
+    
282 214
     
283 215
     // this function fires on DOMready
284 216
     var init = function() {
@@ -296,7 +228,9 @@
296 228
         parseElement: parseElement,
297 229
         defaults: {
298 230
             scope: 'body'
299  
-        }
  231
+        },
  232
+        validators: validators,
  233
+        addDefinition: addDefinition
300 234
     };
301 235
     
302 236
     /**
108  schemas.js
... ...
@@ -0,0 +1,108 @@
  1
+(function($) {
  2
+    if (!jQuery.microdata) {
  3
+        console.error('jQuery Microdata plugin has not been loaded prior to loading this script!');
  4
+        return;
  5
+    }
  6
+    
  7
+    function extend(base, additions) {
  8
+        var ret = base.fields.slice();
  9
+        for (var i = 0; i < additions.length; i++) {
  10
+            if (!findField(additions[i].name))
  11
+                ret.fields.push(additions[i]);
  12
+        }
  13
+        
  14
+        function fieldField(name) {
  15
+            for (var i = 0; i < ret.fields.length; i++) {
  16
+                if (ret.fields[i].name == name) return ret.fields[i];
  17
+            }
  18
+            return false;
  19
+        }
  20
+    }
  21
+    
  22
+    function findByUrl(source, url) {
  23
+        for (var i = 0; i < source.fields.length; i++) {
  24
+            if (source[i].url == url) return source[i];
  25
+        }
  26
+        return null;
  27
+    }
  28
+    
  29
+    
  30
+    var dataVocabulary = [
  31
+        {
  32
+            url: "http://data-vocabulary.org/Event",
  33
+            fields: [
  34
+                 { name: "summary",     required: true,  type: "text",     validator: validators.text     },
  35
+                 { name: "url",         required: false, type: "url",      validator: validators.url      },
  36
+                 { name: "location",    required: false, type: "complex",  validator: validators.complex  }, // optionally represented by data-vocabulary.org/Organization or data-vocabulary.org/Address
  37
+                 { name: "description", required: false, type: "text",     validator: validators.text     },
  38
+                 { name: "startdate",   required: true,  type: "datetime", validator: validators.datetime },
  39
+                 { name: "enddate",     required: false, type: "datetime", validator: validators.datetime },
  40
+                 { name: "duration",    required: false, type: "duration", validator: validators.duration },
  41
+                 { name: "eventtype",   required: false, type: "text",     validator: validators.text     },
  42
+                 { name: "geo",         required: false, type: "complex",  validator: validators.complex  }, // represented by itemscope with two properties: latitude and longitude
  43
+                 { name: "photo",       required: false, type: "url",      validator: validators.url      } 
  44
+            ]
  45
+        },
  46
+        {
  47
+            url: "http://data-vocabulary.org/Person",
  48
+            fields: [
  49
+                 { name: "name",         required: false, type: "text",    validator: validators.text    },
  50
+                 { name: "fn",           required: false, type: "text",    validator: validators.text    }, // alias for "name"
  51
+                 { name: "nickname",     required: false, type: "text",    validator: validators.text    },
  52
+                 { name: "photo",        required: false, type: "url",     validator: validators.url     },
  53
+                 { name: "title",        required: false, type: "text",    validator: validators.text    },
  54
+                 { name: "role",         required: false, type: "text",    validator: validators.text    },
  55
+                 { name: "url",          required: false, type: "url",     validator: validators.url     },
  56
+                 { name: "affiliation",  required: false, type: "text",    validator: validators.text    },
  57
+                 { name: "org",          required: false, type: "text",    validator: validators.text    }, // alias for "affiliation"
  58
+                 { name: "address",      required: false, type: "complex", validator: validators.complex }, // can have subproperties street-address, city, region, postal-code, country-name
  59
+                 { name: "adr",          required: false, type: "complex", validator: validators.complex }, // alias for "address"
  60
+                 { name: "friend",       required: false, type: "url",     validator: validators.url     },
  61
+                 { name: "contact",      required: false, type: "url",     validator: validators.url     },
  62
+                 { name: "acquaintance", required: false, type: "url",     validator: validators.url     }
  63
+                 // NOTE: to define "friend", "contact" or "acquaintance", you can also use XFN rel="..."
  64
+            ]
  65
+        },
  66
+        {
  67
+            url: "http://data-vocabulary.org/Organization",
  68
+            fields: [
  69
+                 { name: "name",    required: false, type: "text",    validator: validators.text    },
  70
+                 { name: "fn",      required: false, type: "text",    validator: validators.text    }, // alias for "name"
  71
+                 { name: "org",     required: false, type: "text",    validator: validators.text    }, // alias for "name"
  72
+                 { name: "url",     required: false, type: "url",     validator: validators.url     },
  73
+                 { name: "address", required: false, type: "complex", validator: validators.complex },
  74
+                 { name: "adr",     required: false, type: "complex", validator: validators.complex }, // alias for "address"
  75
+                 { name: "tel",     required: false, type: "text",    validator: validators.text    },
  76
+                 { name: "geo",     required: false, type: "complex", validator: validators.complex }
  77
+            ]
  78
+        },
  79
+        {
  80
+            url: "http://data-vocabulary.org/Offer",
  81
+            fields: [
  82
+                 { name: "price",           required: false, type: "float",    validator: validators.float    },
  83
+                 { name: "currency",        required: false, type: "text",     validator: validators.currency },
  84
+                 { name: "pricevaliduntil", required: false, type: "datetime", validator: validators.datetime },
  85
+                 { name: "seller",          required: false, type: "complex",  validator: validators.complex  }, // can contain a Person or Organization
  86
+                 {
  87
+                     name: "condition",     required: false, type: "complex",
  88
+                     validator: function(value, el) {
  89
+                         return $(el).hasAttr('content') && $.inArray($(el).attr('content').toLowerCase(), ["new", "used", "refurbished"]);
  90
+                     }
  91
+                 }, 
  92
+                 {
  93
+                     name: "availability",  required: false, type: "complex",
  94
+                     validator: function(value, el) { 
  95
+                         return $(el).hasAttr('content') && $.inArray($(el).attr('content').toLowerCase(), ["out_of_stock", "in_stock", "instore_only", "preorder"]);
  96
+                     }
  97
+                 },
  98
+                 { name: "offerurl",        required: false, type: "url",      validator: validators.url      }, // points to a product web page that includes the offer
  99
+                 { name: "identifier",      required: false, type: "text",     validator: validators.text     }, // recognizes ASIN, ISBN, MPN, UPC, SKU; suggests including product prand and at least one of the identifiers
  100
+                 { name: "itemoffered",     required: false, type: "complex",  validator: validators.complex  }  // can contain free text, a Product or other item types
  101
+            ]
  102
+        }
  103
+    ];
  104
+    
  105
+    for (var i = 0; i < dataVocabulary.length; i++) {
  106
+        $.microdata.addDefinition(dataVocabulary[i].url, dataVocabulary[i].fields);
  107
+    }
  108
+})(jQuery);
1  test.html
@@ -6,6 +6,7 @@
6 6
         <script src="http://code.jquery.com/jquery-latest.js"></script>
7 7
         <script src="http://code.jquery.com/qunit/git/qunit.js"></script>
8 8
         <script src="jquery.microdata.js"></script>
  9
+        <script src="schemas.js"></script>
9 10
         <script src="test.js"></script>
10 11
     </head>
11 12
     <body style="padding-bottom:200px">

0 notes on commit e522df2

Please sign in to comment.
Something went wrong with that request. Please try again.