Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added validator to the tool. Tested in Chrome and IE.

  • Loading branch information...
commit d61614b2331ac22a50b4b8a6c55e22fb44c82f57 1 parent 3bc79ec
@KrofDrakula authored
Showing with 58 additions and 3 deletions.
  1. +58 −3 jquery.microdata.js
View
61 jquery.microdata.js
@@ -13,7 +13,41 @@
"#microdata-container>li.expanded ul { display: block; }\n" +
"#microdata-container li:hover { background: #ff9 }\n" +
"#microdata-container ul { display: none; padding: 0; margin-left: 10px; color: #999 }\n" +
- ".microdata-highlighted { outline: 5px dashed red !important; background: yellow !important };";
+ ".microdata-highlighted { outline: 5px dashed red !important; background: yellow !important }\n" +
+ "#microdata-container .invalid { color: red !important }",
+
+
+ // validators for values in microdata
+ validators = {
+ text: function(value) { return true; },
+ url: function(value) { return /^https?:\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$/.test(value); },
+ int: function(value) { return /^\d+/.test(value); },
+ // TODO: need a proper datetime validator
+ datetime: function(value) { return /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2})?)?$/.test(value); },
+ // TODO: check the duration per http://en.wikipedia.org/wiki/ISO_8601#Durations
+ duration: function(value) { return /^P(([0-9.,]+[YMD])*(T[0-9.,]+[HMS])*|[0-9.,]W)$/.test(value); },
+ // TODO: complex types require other nested entities
+ complex: function(value) { return true; },
+ any: function(value) { return true; }
+ },
+
+
+ // would love it if I could externalize this via cached web service
+ validationRules = {
+ "http://data-vocabulary.org/Event" :
+ [
+ { name: "summary", required: true, type: "text", validator: validators.text },
+ { name: "url", required: false, type: "url", validator: validators.url },
+ { name: "location", required: false, type: "complex", validator: validators.complex }, // optionally represented by data-vocabulary.org/Organization or data-vocabulary.org/Address
+ { name: "description", required: false, type: "text", validator: validators.text },
+ { name: "startdate", required: true, type: "datetime", validator: validators.datetime },
+ { name: "enddate", required: false, type: "datetime", validator: validators.datetime },
+ { name: "duration", required: false, type: "duration", validator: validators.duration },
+ { name: "eventtype", required: false, type: "text", validator: validators.text },
+ { name: "geo", required: false, type: "complex", validator: validators.complex }, // represented by itemscope with two properties: latitude and longitude
+ { name: "photo", required: false, type: "url", validator: validators.url }
+ ]
+ };
/**
* Updates the list of microdata elements on the page
@@ -37,11 +71,32 @@
var addObject = function(element, mdata) {
var type = mdata.type,
t = $('<li title="' + type + '">' + (isUrl(type)? '<a href="' + type + '">' + type.replace(/^.*\//, '') + '</a>': "[no vocabulary]") + '</li>').appendTo(widget),
- u = $('<ul/>').appendTo(t);
+ u = $('<ul/>').appendTo(t), rules = [], required = [], rule, prop, validationExists = false;
if(mdata.properties.length > 0) {
+ if(validationRules.hasOwnProperty(type)) {
+ rules = validationRules[type];
+ required = $.grep(rules, function(item) { return item.required; });
+ validationExists = true;
+ }
for(var i = 0; i < mdata.properties.length; i++) {
- u.append('<li>' + mdata.properties[i].name + ' = ' + mdata.properties[i].value + '</li>');
+ prop = $('<li>' + mdata.properties[i].name + ' = ' + mdata.properties[i].value + '</li>').appendTo(u);
+
+ if(validationExists) {
+ rule = $.grep(rules, function(item) { return item.name == mdata.properties[i].name.toLowerCase(); });
+
+ // pop the field from the required list
+ required = $.grep(required, function(item) { return item.name == mdata.properties[i].name.toLowerCase(); }, true);
+
+ if(rule.length > 0 && !rule[0].validator(mdata.properties[i].value)) {
+ prop.addClass("invalid");
+ }
+ }
+ }
+ if(required.length > 0) {
+ for(var i = 0; i < required.length; i++) {
+ $('<li class="invalid">missing property: ' + required[i].name + '</li>').appendTo(u);
+ }
}
} else {
u.append('[no properties]');
Please sign in to comment.
Something went wrong with that request. Please try again.