Permalink
Browse files

give Valid.Escape some love

  • Loading branch information...
1 parent 02ccdde commit c830583b5b98ad9d04ba94c1e6ce54d20963eeaf @bronson committed Oct 12, 2011
Showing with 28 additions and 16 deletions.
  1. +0 −1 README.md
  2. +5 −3 lib/valid.js
  3. +22 −11 test/valid.test.js
  4. +1 −1 test/validjson.test.js
View
1 README.md
@@ -58,7 +58,6 @@ This library is scary new.
- write an assertion function? Valid.assert(12).integer().min(5);
- convert to using nested functions instead of the `__queue` array?
- 'not' should try to modify the error message on the way through
-- Valid.Escape needs some love
- do a doctest somehow
# Introduction
View
8 lib/valid.js
@@ -53,10 +53,12 @@ Valid.ValidateQueue = function ValidateQueue(queue, value) {
}
};
+// converts value into a string that can be printed
Valid.Escape = function Escape(value) {
- // todo: escape \n, \t, \\, \' and \" in the printed strings
- if(typeof value === 'string') return "'" + value.substring(0,20) + "'";
- return value;
+ if(typeof value === 'string') {
+ return '"' + value.replace(/[\\"]/g, '\\$&').replace(/\u0000/g, '\\0').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t') + '"';
+ }
+ return '' + value;
};
View
33 test/valid.test.js
@@ -70,19 +70,19 @@ Valid.equal().assert(undefined, "equal needs at least one argument");
Valid.equal(null).assert(null);
Valid.equal(null).assert(undefined, "must equal null");
Valid.equal("a").assert("a");
-Valid.equal("a").assert(" ", "must equal 'a'");
+Valid.equal("a").assert(" ", 'must equal "a"');
Valid.equal(12).assert(12);
Valid.equal(12).assert(13, "must equal 12");
Valid.notEqual(null).assert(undefined);
Valid.notEqual(null).assert(null, "can't equal null");
Valid.notEqual("a").assert(" ");
-Valid.notEqual("a").assert("a", "can't equal 'a'");
+Valid.notEqual("a").assert("a", 'can\'t equal "a"');
Valid.notEqual(12).assert(13);
Valid.notEqual(12).assert(12, "can't equal 12");
Valid.equal(undefined, null).assert(null);
Valid.equal(1,2,3,4,5).assert(4);
Valid.equal(1,2,3,4,5).assert(6, "must be 1, 2, 3, 4 or 5");
-Valid.equal("able","baker").assert(undefined, "must be 'able' or 'baker'");
+Valid.equal("able","baker").assert(undefined, 'must be "able" or "baker"');
Valid.oneOf().assert(undefined, "oneOf needs a collection");
Valid.oneOf([1,2,3,4,5]).assert(4);
@@ -208,14 +208,14 @@ Valid.eq('yyz').assert('yyz');
Valid.ge('yyz').assert('yyza');
Valid.ge('yyz').assert('yyz');
Valid.gt('yyz').assert('yyzz');
-Valid.eq('yyz').assert('Yyz', "must equal 'yyz'");
-Valid.ge('yyz').assert('xyza', "must be greater than or equal to 'yyz'");
-Valid.gt('yyz').assert('xyz', "must be greater than 'yyz'");
-Valid.gt('yyz').assert('yyz', "must be greater than 'yyz'");
-Valid.ne('yyz').assert('yyz', "can't equal 'yyz'");
-Valid.lt('yyz').assert('yyza', "must be less than 'yyz'");
-Valid.lt('yyz').assert('yyz', "must be less than 'yyz'");
-Valid.le('yyz').assert('yyzz', "must be less than or equal to 'yyz'");
+Valid.eq("yyz").assert("Yyz", 'must equal "yyz"');
+Valid.ge("yyz").assert("xyza", 'must be greater than or equal to "yyz"');
+Valid.gt("yyz").assert("xyz", 'must be greater than "yyz"');
+Valid.gt("yyz").assert("yyz", 'must be greater than "yyz"');
+Valid.ne("yyz").assert("yyz", 'can\'t equal "yyz"');
+Valid.lt("yyz").assert("yyza", 'must be less than "yyz"');
+Valid.lt("yyz").assert("yyz", 'must be less than "yyz"');
+Valid.le("yyz").assert("yyzz", 'must be less than or equal to "yyz"');
// optional
Valid.optional().assert(undefined); // an optional with no tests is equivalent to nop()
@@ -237,3 +237,14 @@ Valid.equal(3).assert(Valid.undef().nil().number()._queue.length );
// closure leak meant all matches were appended to the same Chain
Valid.equal(2).assert( Valid.match(/^abc$/)._queue.length );
+
+// quickly make sure escape routine works
+if(Valid.Escape(123) !== "123") throw "noint";
+if(Valid.Escape(true) !== "true") throw "notrue";
+if(Valid.Escape(false) !== "false") throw "nofalse";
+if(Valid.Escape(null) !== "null") throw "nonull";
+if(Valid.Escape(undefined) !== "undefined") throw "noundef";
+if(Valid.Escape("a'b'c") !== '"a\'b\'c"') throw "nosquote";
+if(Valid.Escape('a"b"c') !== '"a\\"b\\"c"') throw "nodquote";
+if(Valid.Escape('"\r\n\t\n\r\t') !== '"\\"\\r\\n\\t\\n\\r\\t"') throw "noctrl";
+
View
2 test/validjson.test.js
@@ -85,7 +85,7 @@ Valid.json(null).assert({}, {".": "must be null"});
Valid.json({}).assert(null, {".": "can't be null"});
Valid.json('abc' ).assert('abc');
-Valid.json('abc' ).assert('abc ', {'.': "must equal 'abc'"});
+Valid.json('abc' ).assert('abc ', {'.': 'must equal "abc"'});
Valid.json(123 ).assert(123);
Valid.json(123 ).assert(123.1, {'.': "must equal 123"});
Valid.json(/^abc$/).assert('abc');

0 comments on commit c830583

Please sign in to comment.