Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

get rid of valid() (right after I write tests for it, *sob*)

It's unlikely users will want a validation that throws and,
if they do, they can write their own routine.
  • Loading branch information...
commit a23d26637b3b135312bb29dfd9cf27505b990dcf 1 parent 4694fca
@bronson authored
View
13 README.md
@@ -13,10 +13,8 @@ A lightweight, chaining validation library.
var inRange = Valid.number().min(4).max(9)
inRange.check(3) // check returns true/false, here it returns false
inRange.test(12) // returns "is not less than or equal to 9"
- inRange.odd().verify(6) // throws "6 is not odd"
Valid.optional().string() // success is null, undefined, or a string
- Valid.array(Valid.integer).verify([1,2,3]); // each item in the array must be an integer
var Schema = {
Name: Valid.notBlank(),
@@ -36,7 +34,7 @@ A lightweight, chaining validation library.
}
}
- Valid.json(Schema).verify(data);
+ Valid.json(Schema).check(data);
```
# Gruntles
@@ -58,15 +56,14 @@ use them to test different values:
```javascript
var checker = Valid.integer().even().min(6);
- checker.verify(9); // throws "9 is not even"
- checker.verify(10); // succeeds
+ checker.test(9); // returns "9 is not even"
+ checker.check(10); // returns true
```
Valid offers three ways of testing values:
- test(val) -- returns undefined on success or the error if the validation failed.
- check(val) -- returns true or false.
-- verify(val) -- throws the error if the validation fails.
The error will be a string for simple validations or an object
for JSON validations (see _Errors_ below).
@@ -119,14 +116,14 @@ and add it to the root object:
```javascript
Valid.latitude = Valid.ge(-90).le(90).define();
Valid.longitude = Valid.ge(-180).le(180).define();
- Valid.integer().latitude().verify(20); // success!
+ Valid.integer().latitude().isValid(20); // true!
```
You can also add validations that take parameters:
```javascript
Valid.mod10 = function(rem) { return this.mod(10,rem) }
- Valid.mod10(6).verify(127); // throws "127 mod 10 is 7 not 6"
+ Valid.mod10(6).check(127); // returns "127 mod 10 is 7 not 6"
```
Or just rename them:
View
12 lib/valid.js
@@ -71,9 +71,9 @@ Valid.Escape = function Escape(value) {
// Property 'myfunc' of object function Valid() { } is not a function
// It's really shameful that this function needs to exist.
// 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.
+// In our world, that only works if you don't call it: Valid.null.check(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"
+// Valid.null().check(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() {
@@ -105,14 +105,6 @@ Valid.check = function check(value) {
return !this.test(value);
};
-// throws an error if there were validation errors
-Valid.verify = function assert(value) {
- var error = this.test(value);
- if(error) {
- throw (typeof error === 'string') ? value+" "+error : error;
- }
-};
-
// core validations
View
10 test/valid.test.js
@@ -21,16 +21,6 @@ if(Valid.equal(4).test(4) !== undefined) throw "test() success needs t
if(Valid.equal(4).test(5) !== "is not equal to 4") throw "test() failure needs to return a string";
if(Valid.equal(4).check(4) !== true) throw "check() success needs to return true";
if(Valid.equal(4).check(5) !== false) throw "check() failure needs to return false";
-if(Valid.equal(4).verify(4) !== undefined) throw "verify() success needs to not return anything";
-
-var error;
-try {
- Valid.equal(4).verify(5);
-} catch(e) {
- error = e;
-}
-if(error === undefined) throw "verify() did not throw an error!";
-if(error !== "5 is not equal to 4") throw "verify() failure threw: " + error;
// pathological cases
View
11 test/validjson.test.js
@@ -68,17 +68,6 @@ var result = DeepCompare(Valid.json({a:1}).test({a:2}), {'a': {value: 2, message
if(result) throw "test() failure was wrong: " + result;
if(Valid.json({a:1}).check({a:1}) !== true) throw "check() success needs to return true";
if(Valid.json({a:1}).check({a:2}) !== false) throw "check() failure needs to return false";
-if(Valid.json({a:1}).verify({a:1}) !== undefined) throw "verify() success needs to not return anything";
-
-var error;
-try {
- Valid.json({a:1}).verify({a:2});
-} catch(e) {
- error = e;
-}
-if(error === undefined) throw "verify() did not throw an error!";
-result = DeepCompare(error, {'a': {value: 2, message: 'does not equal 1'}});
-if(result !== undefined) { throw "verify() failure: " + result; }
// now ensure Valid.json works
Please sign in to comment.
Something went wrong with that request. Please try again.