Permalink
Browse files

minor cleanups

  • Loading branch information...
bronson committed Sep 21, 2011
1 parent 7e94b6e commit bdbc4b8c01e19923b55e0b66499ed757df77e97f
Showing with 30 additions and 21 deletions.
  1. +2 −5 lib/valid-engine.js
  2. +6 −9 lib/valid.js
  3. +2 −1 lib/validjson.js
  4. +8 −4 test/deepcompare.js
  5. +5 −1 test/valid.test.js
  6. +7 −1 test/validjson.test.js
View
@@ -1,9 +1,6 @@
-// valid-engine.js Scott Bronson 2011
+// valid-engine.js
// This file defines the core objects that make Valid run.
-// See valid.js for the default set of tests.
-
-
-// todo? is it possible to turn test objects into arrays?
+// Your code should require valid.js or validjson.js.
var Valid = function Valid() { };
module.exports = Valid;
View
@@ -1,8 +1,5 @@
-// valid.js Scott Bronson 2011
-// This file defines the Valid object and some core validation tests.
-
-// todo: Valid.length(2,4).array().integer()?
-// todo? is it possible to turn test objects into arrays?
+// valid.js
+// This file contains the default Valid tests but not the JSON schema verifier.
var Valid = require('./valid-engine');
module.exports = Valid;
@@ -35,13 +32,13 @@ Valid.optional = Valid.SimpleTest(function Optional(value) { if(value === null |
Valid.equal = Valid.SimpleTest(function Equal(value) {
// Here is the old equal, not sure supporting multiple values is worth the additional complexity...
// Valid.equal = Valid.SimpleTest(function Equal(val,want) { if(val !== want) return "is not equal to "+Valid.Escape(want); });
- if(arguments.length == 1) return "equal needs at least one argument";
+ if(arguments.length === 1) return "equal needs at least one argument";
var opts = [];
- for(i=1; i<arguments.length; i++) {
+ for(var i=1; i<arguments.length; i++) {
if(value === arguments[i]) return;
opts.push(Valid.Escape(arguments[i]));
}
- if(arguments.length == 2) return "is not equal to " + opts[0];
+ if(arguments.length === 2) return "is not equal to " + opts[0];
var lastopt = opts.pop();
return "is not " + opts.join(", ") + " or " + lastopt;
});
@@ -144,7 +141,7 @@ Valid['true'] = Valid.equal(true).define();
Valid['false'] = Valid.equal(false).define();
Valid['function'] = Valid.type('function').define();
Valid['in'] = Valid.oneOf;
-Valid['length'] = Valid.len;
+Valid.length = Valid.len;
Valid.todo = function(name) { return this.fail((name ? name : "this") + " is still todo"); };
Valid.notEqual = function(arg) { return Valid.not(Valid.equal(arg), "is equal to " + Valid.Escape(arg)); };
View
@@ -1,4 +1,5 @@
-// Returns a full Valid environment that includes json comparison
+// validjson.js
+// Returns a full Valid environment including json comparison
var Valid = require('./valid');
module.exports = Valid;
View
@@ -1,10 +1,14 @@
// deepcompare.js
// Quickly compare two JavaScript values.
//
+// usage:
+// DeepCompare({a:1}, {a:1}) returns undefined meaning no differences
+// DeepCompare({a:1}, {a:2}) returns a string describing the difference
+//
// can't use JSON.stringify for comparison because of key order problems
-// returns undefined if equal or a string describing the location of the difference.
-module.exports = function DeepCompare(path, a, b) {
+module.exports = function DeepCompare(a, b, path) {
+ if(path === undefined) path = '';
if(typeof a !== typeof b) return path + ": " + (typeof a) + " vs " + (typeof b);
switch(typeof a) {
case 'string': case 'number': case 'boolean': case 'undefined':
@@ -18,15 +22,15 @@ module.exports = function DeepCompare(path, a, b) {
if(!(b instanceof Array)) return path + ": should be an Array";
if(a.length !== b.length) return path + " should be length " + a.length + " not " + b.length;
for(var i=0; i < a.length; i++) {
- var iresult = DeepCompare(path+"["+i+"]", a[i], b[i]);
+ var iresult = DeepCompare(a[i], b[i], path+"["+i+"]");
if(iresult) return iresult;
}
} else {
if(b instanceof Array) return path + ": should not be an Array";
for(var akey in a) {
if(!a.hasOwnProperty(akey)) continue;
if(!(akey in b)) return path + ": " + akey + " is missing";
- var aresult = DeepCompare(path+"."+akey, a[akey], b[akey]);
+ var aresult = DeepCompare(a[akey], b[akey], path+"."+akey);
if(aresult) return aresult;
}
for(var bkey in b) {
View
@@ -1,6 +1,10 @@
+// valid.test.js
+// Tests Valid's default set of validations
+
var Valid = require('../lib/valid');
-// Like check() but throws if the result doesn't match the expectation
+
+// throws an error if the result doesn't exactly match the expectation
Valid.assert = function assert(value, expected) {
var actual = this.test(value);
if(expected !== actual) {
View
@@ -1,9 +1,14 @@
+// validjson.test.js
+// Tests Valid's json schema validator
+
var Valid = require('../lib/validjson');
var DeepCompare = require('./deepcompare');
+
+// throws an error unless the result exactly matches what is expected
Valid.assert = function assert(value, expected) {
var actual = this.test(value);
- var diffstr = DeepCompare("", expected, actual);
+ var diffstr = DeepCompare(expected, actual);
if(diffstr) {
var exstr = (expected === undefined ? "success" : JSON.stringify(expected));
var acstr = (actual === undefined ? "success" : JSON.stringify(actual));
@@ -55,6 +60,7 @@ if(false) Valid.json(PersonSchema).assert({ // now validate this JSON
});
+
Valid.json(true ).assert(true);
Valid.json(false).assert(false);
Valid.json(true ).assert(false, {'.': {message: "does not equal true", value: false}});

0 comments on commit bdbc4b8

Please sign in to comment.