Permalink
Browse files

Add support for 'Advanced Properties'

  • Loading branch information...
1 parent 911abd5 commit 40404f7dc1e37c8f9a2d3516203d6d724469c504 @jviereck committed Feb 7, 2011
Showing with 103 additions and 4 deletions.
  1. +78 −0 test.js
  2. +25 −4 validate.js
View
78 test.js
@@ -345,6 +345,84 @@ test(arr, V.Array({
}
}), V.ERROR_STRICT /*FAIL*/, "Test 4");
+/** ===========================================================================
+ * The last test is valid, but it's a little bit hard to figure out in which
+ * cases the region and in which cases the text property is validated. You can
+ * rewrite this as the following which should be much more cleaner to read.
+ */
+console.log("== Advanced Properties ==");
+
+test(arr, V.Array(
+ V.ObjectOnlyIf("action", "insert", {
+ id: V.Integer,
+ action: V.String,
+ text: V.String
+ }),
+ V.ObjectOnlyIf("action", "update", {
+ id: V.Integer,
+ action: V.String,
+ region: V.Integer
+ })
+), V_ERROR_NONE, "Multiple object structures 1");
+
+// Does 100% the same as the last test, but don't use any shortcuts.
+test(arr, {
+ type: "array",
+ each: [{
+ type: "object",
+ onlyIf: function(obj) {
+ return obj.action == "insert";
+ },
+ properties: {
+ id: V.Integer,
+ action: V.String,
+ text: V.String
+ }
+ }, {
+ type: "object",
+ onlyIf: function(obj) {
+ return obj.action == "update";
+ },
+ properties: {
+ id: V.Integer,
+ action: V.String,
+ region: V.Integer
+ }
+ }]
+}, V_ERROR_NONE, "Multiple object structures 2");
+
+// Still the same, using some shortcuts.
+test(arr, V.Array({
+ type: "object",
+ onlyIf: function(obj) {
+ return obj.action == "insert";
+ },
+ properties: {
+ id: V.Integer,
+ action: V.String,
+ text: V.String
+ }
+}, {
+ type: "object",
+ onlyIf: function(obj) {
+ return obj.action == "update";
+ },
+ properties: {
+ id: V.Integer,
+ action: V.String,
+ region: V.Integer
+ }
+}), V_ERROR_NONE, "Multiple object structures 3");
+
+// This will fail, as there is no rule to validate the "update" action
+test(arr, V.Array(
+ V.ObjectOnlyIf("action", "insert", {
+ id: V.Integer,
+ action: V.String,
+ text: V.String
+ })
+), V.ERROR_STRICT, "Multiple object structures 4");
+
/** ===========================================================================
* Let's hope all tests passed!
*/
View
@@ -319,11 +319,32 @@ V.Object.$default = {
type: "object"
};
-V.Array = function(eachObjectProperties) {
+V.ObjectOnlyIf = function(key, value, properties) {
return {
- type: "array",
- eachObject: eachObjectProperties
- };
+ type: "object",
+ onlyIf: function(obj) {
+ return obj[key] == value;
+ },
+ properties: properties
+ }
+}
+
+V.Array = function() {
+ if (arguments.length == 1 && arguments[0].type != "object") {
+ return {
+ type: "array",
+ eachObject: arguments[0]
+ };
+ } else {
+ var args = [];
+ for (var i = 0; i < arguments.length; i++) {
+ args.push(arguments[i]);
+ }
+ return {
+ type: "array",
+ each: args
+ };
+ }
};
V.Array.$default = {

0 comments on commit 40404f7

Please sign in to comment.