Skip to content

Commit

Permalink
get rid of valid() (right after I write tests for it, *sob*)
Browse files Browse the repository at this point in the history
It's unlikely users will want a validation that throws and,
if they do, they can write their own routine.
  • Loading branch information
bronson committed Oct 4, 2011
1 parent 4694fca commit a23d266
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 39 deletions.
13 changes: 5 additions & 8 deletions README.md
Expand Up @@ -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(),
Expand All @@ -36,7 +34,7 @@ A lightweight, chaining validation library.
}
}

Valid.json(Schema).verify(data);
Valid.json(Schema).check(data);
```

# Gruntles
Expand All @@ -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).
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 2 additions & 10 deletions lib/valid.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions test/valid.test.js
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions test/validjson.test.js
Expand Up @@ -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
Expand Down

0 comments on commit a23d266

Please sign in to comment.