Permalink
Browse files

messageFor(v,msg) -> v.message(msg)

  • Loading branch information...
1 parent 99bddac commit f4c90f5be695e165f52609583f6f7c84feb02cb3 @bronson committed Oct 4, 2011
Showing with 35 additions and 54 deletions.
  1. +8 −1 README.md
  2. +12 −9 lib/valid.js
  3. +13 −42 test/valid.test.js
  4. +2 −2 test/validjson.test.js
View
@@ -16,7 +16,9 @@ A lightweight, chaining validation library.
Valid.optional().string().check(null); // can be null, undefined, or a string
Valid.array(Valid.integer).check([1,2,3]); // checks each item in the array
+ var isHex = Valid.match(/^[0-9A-F]$/i).message("should be a hexadecimal number");
+ // test JSON structures:
var Schema = {
Name: Valid.notBlank(),
Address: {
@@ -35,6 +37,10 @@ A lightweight, chaining validation library.
}
Valid.json(Schema).check(data);
+
+ // Easily define your own validations:
+ Valid.isPowerOfTen = Valid.mod(10).message("should be a power of ten).define();
+ Valid.min(10).max(100).isPowerOfTen().check(50);
```
# Gruntles
@@ -48,6 +54,7 @@ This library is scary new.
- noexisty is a stupid name
- covert to Rails-like "5 should equal 4" instead of current "5 is not equal to 4"?
- Allow putting value first? i.e. Valid(9).lt(12).gt(10) throws "9 is not greater than 10"
+- convert to using nested functions instead of the `__queue` array?
# Introduction
@@ -77,7 +84,7 @@ See [valid.js](https://github.com/bronson/valid/blob/master/lib/valid.js).
- Strings: string(), len(min,max), blank(), notBlank()
- Regexps: match(regex[,modifiers]), nomatch(regex[,modifiers])
- Logic: and(test[,test...]), or(test[,test...]), not(test,message)
-- Utilities: nop(), fail([message]), messageFor(test,message), todo([test])
+- Utilities: nop(), fail([message]), message(msg), todo([test])
- JSON: json(schema)
\*: These are JavaScript keywords. While `Valid.undefined()` works
View
@@ -158,10 +158,13 @@ Valid.len = Valid.SimpleValidation(function Len(value,min,max) {
}
});
-Valid.messageFor = Valid.SimpleValidation(function Msg(value, validation, message) {
- var error = this.ValidateQueue(validation._queue, value);
- if(error) return message;
-});
+Valid.message = function message(msg) {
+ var validation = this.GetChain();
+ return Valid.AddValidation(function Message(value) {
+ var error = this.ValidateQueue(validation._queue, value);
+ if(error) return validation._queue ? msg : error;
+ }, msg);
+}
Valid.not = Valid.SimpleValidation(function Not(value, validation, message) {
var error = this.ValidateQueue(validation._queue, value);
@@ -209,16 +212,16 @@ Valid.nil = Valid.equal(null).define();
Valid.notNull = Valid.not(Valid.nil(), "is null").define();
Valid.noexisty = Valid.equal(undefined, null).define();
Valid.exists = Valid.not(Valid.noexisty(), "does not exist").define();
-Valid.empty = Valid.messageFor(Valid.optional().len(0,0), "is not empty").define();
+Valid.empty = Valid.len(0,0).message("should be empty").define();
Valid.boolean = Valid.type('boolean').define();
Valid.isTrue = Valid.equal(true).define();
Valid.isFalse = Valid.equal(false).define();
Valid.number = Valid.type('number').define();
-Valid.integer = Valid.number().messageFor(Valid.mod(1), "is not an integer").define();
-Valid.even = Valid.number().messageFor(Valid.mod(2), "is not even").define();
-Valid.odd = Valid.number().messageFor(Valid.mod(2,1), "is not odd").define();
+Valid.integer = Valid.number().mod(1).message("is not an integer").define();
+Valid.even = Valid.number().mod(2).message("is not even").define();
+Valid.odd = Valid.number().mod(2,1).message("is not odd").define();
Valid.string = Valid.type('string').define();
-Valid.blank = Valid.messageFor(Valid.optional().match(/^\s*$/), "is not blank").define();
+Valid.blank = Valid.optional().match(/^\s*$/).message("is not blank").define();
Valid.notBlank = Valid.not(Valid.blank(), "is blank").define();
Valid.object = Valid.type('object').define();
// reserved words, calling them with dot notation may cause problems with crappy JS implementations
View
@@ -22,11 +22,16 @@ if(Valid.equal(4).check(5) !== "is not equal to 4") throw "test() failure needs
if(Valid.equal(4).isValid(4) !== true) throw "check() success needs to return true";
if(Valid.equal(4).isValid(5) !== false) throw "check() failure needs to return false";
-
-// pathological cases
Valid.assert("any value", "no validations!");
+
+// utilities
Valid.nop().assert("any value"); // no-op always succeeds
Valid.fail("die!").assert("anything", "die!"); // fail always fails
+Valid.todo("release 1.0").assert(12, 'release 1.0 is still todo');
+Valid.equal(12).message("should be 12").assert(12)
+Valid.equal(12).message("should be 12").assert(13, "should be 12")
+Valid.equal(12).message("first").message("last").assert(13, "last");
+Valid.message("bla").assert(12, "no validations!");
// undefined
Valid['undefined']().assert(undefined);
@@ -108,7 +113,7 @@ Valid.number().assert(123);
Valid.number().assert('123', "is of type string not number");
Valid.number().assert(undefined, "is of type undefined not number");
Valid.integer().assert(123.0);
-Valid.integer().assert('123.0', "is of type string not number");
+Valid.integer().assert('123.0', "is not an integer");
Valid.integer().assert(123.1, "is not an integer");
Valid.even().assert(0);
Valid.even().assert(12);
@@ -143,7 +148,7 @@ Valid.len(1,4).assert(' . . . . ', 'has length 9, greater than 4');
Valid.len(2,4).assert(undefined, "doesn't have a length field");
Valid.len(2,4).assert(3, "doesn't have a length field");
Valid.empty().assert('');
-Valid.empty().assert(' ', 'is not empty');
+Valid.empty().assert(' ', 'should be empty');
// regexes
Valid.match(/^.*$/).assert('');
@@ -161,13 +166,13 @@ Valid.nomatch(/^\s*|\s*$/, 'i').assert('doh ', "matches /^\\s*|\\s*$/");
// arrays
Valid.array().empty().assert([]);
Valid.array().empty().assert(null, "is not an array");
-Valid.array().empty().assert([undefined], "is not empty");
+Valid.array().empty().assert([undefined], "should be empty");
Valid.len(0).assert([]);
Valid.len(0,0).assert([]);
Valid.len(0,0).assert([undefined], "has length 1, greater than 0");
Valid.len(6,9).assert([0,1,2,3,4,5]);
Valid.array(Valid.integer()).assert([0,1,2]);
-Valid.array(Valid.defined().integer()).assert([0,null,2], "item 1 is of type object not number");
+Valid.array(Valid.defined().integer()).assert([0,null,2], "item 1 is not an integer");
Valid.array(Valid.defined().string().match(/cow/)).assert(['cow','cowtown','vacaville'], "item 2 does not match /cow/");
// logic
@@ -228,7 +233,7 @@ Valid.optional().assert(undefined); // an optional with no tests is equivalent
Valid.optional().integer().assert(undefined);
Valid.optional().integer().assert(null);
Valid.optional().integer().assert(12);
-Valid.optional().integer().assert("12", "is of type string not number");
+Valid.optional().integer().assert("12", "is not an integer");
// static
var nullOrString = Valid.or(Valid.nil(), Valid.string());
@@ -237,43 +242,9 @@ nullOrString.assert('123');
nullOrString.assert(123, "is not equal to null and is of type number not string");
+
// closure leak meant only one test would be queued, not all three
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 );
-/*
-schema( true ).validate( true ).result();
-schema( false ).validate( false ).result();
-schema( true ).validate( false ).result("false is not true");
-schema( false ).validate( true ).result("true is not false");
-schema( false ).validate(undefined).result("undefined is not false");
-
-schema( 'abc' ).validate( 'abc' ).result();
-schema( 'abc' ).validate( 'abc ' ).result("'abc ' does not equal 'abc'");
-schema( 123 ).validate( 123 ).result();
-schema( 123 ).validate( 123.1 ).result("123.1 does not equal 123");
-schema( /^abc$/ ).validate( 'abc' ).result();
-schema( /^abc$/ ).validate( 'abcd' ).result("'abcd' does not match /^abc$/");
-
-schema( {abc: 123} ).validate( {abc: 123} ).result();
-schema( {abc: 123, def: 456} ).validate( {abc: 123} ).result("[object Object] is missing def");
-schema( {abc: 123} ).validate( {abc: 123, def: 456} ).result("[object Object] has def but template does not");
-
-schema( {a: {b: {c: 1}}} ).validate( {a: {b: {c: 2}}} ).result("a,b,c: 2 does not equal 1 for [object Object]"); // TODO: improve error message
-
-// arrays
-schema( [12, 13] ).validate( [12, 13] ).result();
-schema( [12, 13] ).validate( [12, 14] ).result("1: 14 does not equal 13 for 12,14"); // TODO improve error message
-schema( [12, 13] ).validate( 12 ).result("12 is not an Array");
-schema( [12, 13] ).validate( undefined ).result("undefined is not an Array");
-schema( [12, Validator.IsInteger] ).validate( [12, 13] ).result();
-
-var _ = Validator;
-schema( _.IsArray(_.IsInteger) ).validate([12, 13, 14]).result();
-schema( _.IsArray(_.IsInteger, {min: 1, max: 3}) ).validate([12, 13]).result();
-schema( _.IsArray(_.IsString, {min: 1, max: 3}) ).validate([12, 13]).result(["0: 12 is number, not string for 12,13", "1: 13 is number, not string for 12,13"]); // TODO: improve error message
-schema( _.IsArray(_.IsInteger, {min: 3, max: 4}) ).validate([12, 13]).result("12,13 has fewer than 3 elements");
-schema( _.IsArray(_.IsInteger, {min: 1, max: 0}) ).validate([12, 13]).result("12,13 has more than 0 elements");
-
-*/
View
@@ -123,9 +123,9 @@ Valid.json([12, 13]).assert(null, {".": {message: "is null", value: null}
Valid.json(Valid.array() ).assert([]);
Valid.json(Valid.array() ).assert([1,2,3]);
Valid.json(Valid.array(Valid.integer())).assert([1,2,3]);
-Valid.json(Valid.array(Valid.integer())).assert([1,2,'3'], {".": {message: "item 2 is of type string not number", value: [1,2,"3"]}});
+Valid.json(Valid.array(Valid.integer())).assert([1,2,'3'], {".": {message: "item 2 is not an integer", value: [1,2,"3"]}});
Valid.json(Valid.array() ).assert({"1":"2"}, {".": {message: "is not an array", value: {"1":"2"}}});
Valid.json(Valid.array() ).assert(null, {".": {message: "is not an array", value: null}});
Valid.json([12, Valid.integer()]).assert([12, 13]);
-Valid.json([12, Valid.integer()]).assert([12, "13"], {"[1]": {message: "is of type string not number", value: "13"}});
+Valid.json([12, Valid.integer()]).assert([12, "13"], {"[1]": {message: "is not an integer", value: "13"}});

0 comments on commit f4c90f5

Please sign in to comment.