Browse files

.isNull() -> .null(), etc

  • Loading branch information...
1 parent b0f1535 commit 7372b45849611467b24c44b21d719be0ff1b72fb @bronson committed Sep 19, 2011
Showing with 61 additions and 55 deletions.
  1. +42 −35 test-valid.js
  2. +19 −20 valid.js
View
77 test-valid.js
@@ -12,16 +12,16 @@ Valid.assert = function assert(value, expected) {
Valid.assert("any value", "no tests!");
-Valid.isUndefined().assert(undefined);
-Valid.isUndefined().assert(1, "is not equal to undefined");
-Valid.isUndefined().assert(null, "is not equal to undefined");
+Valid.undefined().assert(undefined);
+Valid.undefined().assert(1, "is not equal to undefined");
+Valid.undefined().assert(null, "is not equal to undefined");
Valid.defined().assert(undefined, "is undefined");
Valid.defined().assert(1);
Valid.defined().assert(null);
-Valid.isNull().assert(null);
-Valid.isNull().assert(1, "is not equal to null"); // "is not null" would sound better
-Valid.isNull().assert(undefined, "is not equal to null");
+Valid.null().assert(null);
+Valid.null().assert(1, "is not equal to null"); // "is not null" would sound better
+Valid.null().assert(undefined, "is not equal to null");
Valid.notNull().assert(null, "is null");
Valid.notNull().assert(1);
Valid.notNull().assert(undefined);
@@ -35,42 +35,49 @@ Valid.exists().assert(undefined, "does not exist");
// closure leak meant only one test would be queued, not all three
Valid.equal(null).assert(null);
Valid.equal(null).assert(undefined, "is not equal to null");
-Valid.equal(3).assert(Valid.isUndefined().isNull().isNumber()._queue.length );
+Valid.equal(3).assert(Valid.undefined().null().number()._queue.length );
Valid.notEqual(null).assert(undefined);
Valid.notEqual(null).assert(null, "is equal to null");
Valid.typeOf('undefined').assert(undefined);
-// typeof null returns 'object' on some JS implementations, use isNull()
+// typeof null returns 'object' on some JS implementations, use null()
Valid.typeOf('number').assert(123);
Valid.typeOf('string').assert('123');
Valid.typeOf('garbage').assert('123', "is of type string not garbage");
Valid.typeOf(undefined).assert(undefined, "typeOf requires a string argument, not undefined");
Valid.typeOf(123).assert(123, "typeOf requires a string argument, not number");
// booleans
-Valid.isBoolean().assert(true);
-Valid.isBoolean().assert(false);
-Valid.isBoolean().assert(undefined, "is of type undefined not boolean");
-Valid.isTrue().assert(true);
-Valid.isTrue().assert(false, "is not equal to true");
-Valid.isFalse().assert(false);
-Valid.isFalse().assert(true, "is not equal to false");
+Valid.boolean().assert(true);
+Valid.boolean().assert(false);
+Valid.boolean().assert(undefined, "is of type undefined not boolean");
+Valid.true().assert(true);
+Valid.true().assert(false, "is not equal to true");
+Valid.false().assert(false);
+Valid.false().assert(true, "is not equal to false");
// numbers
-Valid.isNumber().assert(123);
-Valid.isNumber().assert('123', "is of type string not number");
-Valid.isNumber().assert(undefined, "is of type undefined not number");
-Valid.isInteger().assert(123.0);
-Valid.isInteger().assert('123.0', "is of type string not number");
-Valid.isInteger().assert(123.1, "is not an integer");
+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.1, "is not an integer");
// strings
-Valid.isString().assert(' 123');
-Valid.isString().assert(undefined, "is of type undefined not string");
+Valid.string().assert(' 123');
+Valid.string().assert(undefined, "is of type undefined not string");
Valid.blank().assert(' \n\t ');
Valid.blank().assert('');
Valid.blank().assert(null);
+Valid.blank().assert(undefined);
+Valid.blank().assert(' .', "is not blank");
+Valid.notBlank().assert(' bla');
+Valid.notBlank().assert('\n', "is blank");
+Valid.notBlank().assert('', "is blank");
+Valid.notBlank().assert(null, "is blank");
+Valid.notBlank().assert(undefined, "is blank");
// regexes
Valid.match(/^.*$/).assert('');
@@ -81,22 +88,22 @@ Valid.match(/1/).assert(1, "is of type number not string");
Valid.equal(2).assert( Valid.match(/^abc$/)._queue.length ); // closure leak meant all matches were appended to the same Chain
Valid.and().assert(null); // passing 0 tests succeeds unconditionally
-Valid.and( Valid.isNull() ).assert(null); // passing 1 arg success
-Valid.and( Valid.isNull() ).assert(undefined, "is not equal to null"); // passing 1 arg failure
+Valid.and( Valid.null() ).assert(null); // passing 1 arg success
+Valid.and( Valid.null() ).assert(undefined, "is not equal to null"); // passing 1 arg failure
Valid.and( Valid.typeOf('string'), Valid.match(/c$/), Valid.match(/^a/) ).assert('abc');
Valid.and( Valid.typeOf('string'), Valid.match(/^bbc$/) ).assert('abc', "does not match /^bbc$/");
Valid.and( Valid.typeOf('number'), Valid.match(/^abc$/) ).assert('abc', "is of type string not number");
Valid.or().assert(undefined); // passing 0 tests succeeds unconditionally
-Valid.or( Valid.isNull() ).assert(null); // passing 1 arg success
-Valid.or( Valid.isNull() ).assert(undefined, "is not equal to null"); // passing 1 arg failure
-Valid.or( Valid.isNull(), Valid.isUndefined() ).assert(undefined);
-Valid.or( Valid.isNull(), Valid.isUndefined() ).assert(null);
-Valid.or( Valid.isNull(), Valid.isNumber(), Valid.isString() ).assert('mosdef');
-Valid.or( Valid.isUndefined(), Valid.match(/^abc$/), Valid.match(/def$/) ).assert('mosdef');
+Valid.or( Valid.null() ).assert(null); // passing 1 arg success
+Valid.or( Valid.null() ).assert(undefined, "is not equal to null"); // passing 1 arg failure
+Valid.or( Valid.null(), Valid.undefined() ).assert(undefined);
+Valid.or( Valid.null(), Valid.undefined() ).assert(null);
+Valid.or( Valid.null(), Valid.number(), Valid.string() ).assert('mosdef');
+Valid.or( Valid.undefined(), Valid.match(/^abc$/), Valid.match(/def$/) ).assert('mosdef');
Valid.or( Valid.typeOf('number'), Valid.match(/^bbc$/) ).assert('abc', "is of type string not number and does not match /^bbc$/");
-var nullOrString = Valid.or(Valid.isNull(), Valid.isString());
+var nullOrString = Valid.or(Valid.null(), Valid.string());
nullOrString.assert(null);
nullOrString.assert('123');
nullOrString.assert(123, "is not equal to null and is of type number not string");
@@ -113,9 +120,9 @@ schema( Validator.IsNotBlank ).validate(' a').result("' a' has leading whites
schema( Validator.IsNotBlank ).validate('').result("'' can't be blank");
*/
-Valid.optional(Valid.isInteger()).assert(undefined);
-Valid.optional(Valid.isInteger()).assert(12);
-Valid.optional(Valid.isInteger()).assert("12", "is optional and is of type string not number");
+Valid.optional(Valid.integer()).assert(undefined);
+Valid.optional(Valid.integer()).assert(12);
+Valid.optional(Valid.integer()).assert("12", "is optional and is of type string not number");
/*
schema( true ).validate( true ).result();
View
39 valid.js
@@ -1,7 +1,6 @@
// valid.js Scott Bronson 2011
// This file defines the Valid object and some core validation tests.
-// todo? type instead of typeOf boolean instead of isBoolean
// todo? is it possible to turn test objects into arrays?
var Valid = function Valid() { };
@@ -74,10 +73,10 @@ Valid.verify = function assert(value) {
// then you forgot to call define().
//
// It's really shameful that this function needs to exist.
-// In an ideal world you could just do this: Valid.isNull() = Valid.equal(null);
-// In our world, that only works if you don't call it: Valid.isNull.verify(1); Ugh.
-// Since Valid.equal(null) returns the chain object, if you call isNull:
-// Valid.isNull().verify(1) JS complains "Property isNull is not a function"
+// In an ideal world you could just do this: Valid.null() = Valid.equal(null);
+// In our world, that only works if you don't call it: Valid.null.verify(1); Ugh.
+// Since Valid.equal(null) returns the chain object, if you call null:
+// Valid.null().verify(1) JS complains "Property null is not a function"
// For this to work, JS needs to a callable object with a prototype chain.
// And, without using nonstandard __proto__, I don't think that's possible...?
Valid.define = function define() {
@@ -140,7 +139,7 @@ Valid.or = function or() {
Valid.match = function match(pattern, modifiers) {
if(typeof pattern !== 'function') pattern = new RegExp(pattern, modifiers);
- return this.isString().AddTest( function Match(value) {
+ return this.string().AddTest( function Match(value) {
if(!value.match(pattern)) return "does not match " + pattern;
});
};
@@ -152,23 +151,23 @@ Valid.todo = function(name) {
return this.fail((name ? name : "this") + " is still todo");
};
-Valid.isUndefined = Valid.equal(undefined).define();
-Valid.defined = Valid.not(Valid.isUndefined(), "is undefined").define();
-Valid.isNull = Valid.equal(null).define();
-Valid.notNull = Valid.not(Valid.isNull(), "is null").define();
+Valid.undefined = Valid.equal(undefined).define();
+Valid.defined = Valid.not(Valid.undefined(), "is undefined").define();
+Valid.null = Valid.equal(null).define();
+Valid.notNull = Valid.not(Valid.null(), "is null").define();
Valid.exists = Valid.messageFor(Valid.defined().notNull(), "does not exist").define();
Valid.noexisty = Valid.not(Valid.exists(), "exists").define();
-Valid.isBoolean = Valid.typeOf('boolean').define();
-Valid.isTrue = Valid.equal(true).define();
-Valid.isFalse = Valid.equal(false).define();
-Valid.isNumber = Valid.typeOf('number').define();
-Valid.isInteger = Valid.isNumber().messageFor(Valid.mod(1), "is not an integer").define();
-Valid.isString = Valid.typeOf('string').define();
+Valid.boolean = Valid.typeOf('boolean').define();
+Valid.true = Valid.equal(true).define();
+Valid.false = Valid.equal(false).define();
+Valid.number = Valid.typeOf('number').define();
+Valid.integer = Valid.number().messageFor(Valid.mod(1), "is not an integer").define();
+Valid.string = Valid.typeOf('string').define();
Valid.blank = Valid.messageFor(Valid.or(Valid.noexisty(),Valid.match(/^\s*$/)), "is not blank").define();
-Valid.nonBlank = Valid.not(Valid.blank(), "is blank").define();
-Valid.isFunction = Valid.typeOf('function').define();
-Valid.isObject = Valid.typeOf('object').define();
+Valid.notBlank = Valid.not(Valid.blank(), "is blank").define();
+Valid.function = Valid.typeOf('function').define();
+Valid.object = Valid.typeOf('object').define();
-Valid.optional = function(test) { return Valid.or(Valid.messageFor(Valid.isUndefined(),"is optional"), test); };
+Valid.optional = function(test) { return Valid.or(Valid.messageFor(Valid.undefined(),"is optional"), test); };
Valid.notEqual = function(arg) { return Valid.not(Valid.equal(arg), "is equal to " + arg); };

0 comments on commit 7372b45

Please sign in to comment.