Permalink
Browse files

readme & comment fixes

  • Loading branch information...
1 parent 0e98447 commit 84f994a11637857c13b65bcdb98b8b1d0dfc1438 @bronson committed Oct 2, 2011
Showing with 50 additions and 4 deletions.
  1. +38 −1 README.md
  2. +3 −1 console.js
  3. +4 −0 lib/valid.js
  4. +2 −1 test/valid.test.js
  5. +3 −1 test/validjson.test.js
View
39 README.md
@@ -45,8 +45,26 @@ This library is scary new.
- Valid is not a great name. it's not even a noun.
- noexisty is a stupid name
- Allow putting value first? i.e. Valid(9).lt(12).gt(10) throws "9 is not greater than 10"
-- Allow returning multiple errors for a single field, like Rails validations?
+# Introduction
+
+Valid allows you to declare strings of validations and
+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
+```
+
+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 error object
+for JSON validations (see _Errors_ below).
# Built-In Validations
@@ -113,6 +131,25 @@ Or just rename them:
```
+# Weirdness
+
+On Node 0.4.x, if the console tries to print a Valid chain, you
+get this error:
+
+ > Valid.integer()
+ TypeError: Function.prototype.toString is not generic
+ at Function.toString (native)
+ at Array.toString (native)
+
+It's a Node bug. 0.5.x does the correct thing and prints the
+chain:
+
+ > Valid.integer()
+ { _queue:
+ [ { [Function: SimpleTest] data: [Object] },
+ { [Function: SimpleTest] data: [Object] } ] }
+
+
# Alternatives
<https://github.com/chriso/node-validator>
View
4 console.js
@@ -1,5 +1,7 @@
#!/usr/bin/env node
-// fires up a console with Valid loaded
+// Fires up a console with Valid loaded.
+// Allows you to quickly play with Valid on the console.
+
Valid = require('./lib/valid');
require('repl').start();
View
4 lib/valid.js
@@ -1,6 +1,7 @@
// valid.js
//
// todo: change error messages to "can't be blank", "must equal blah", etc.
+// todo: can I make a shorter syntax to change the error message?
// todo: pass a json schema to array()? factor into RunSubtest & have everything call this.
// todo: dates
// todo: npm publish
@@ -93,15 +94,18 @@ Valid.define = function define() {
// results
+// returns the error string or undefined if there were no validation errors
Valid.test = function test(value) {
var self = this.GetChain();
return self.GetChain().ValidateQueue(self._queue, value);
};
+// returns true if the validation succeeded, false if there were errors
Valid.check = function check(value) {
return !this.test(value);
};
+// throws an error if there were validation errors
Valid.verify = function assert(value) {
var message = this.test(value);
if(message) throw value + " " + message;
View
3 test/valid.test.js
@@ -1,5 +1,6 @@
// valid.test.js
-// Tests Valid's default set of validations
+//
+// Tests non-JSON validations that return string errors.
var Valid = require('../lib/valid');
View
4 test/validjson.test.js
@@ -1,5 +1,7 @@
// validjson.test.js
-// Tests Valid's json schema validator
+//
+// Tests JSON validations that return an Error object
+
var Valid = require('../lib/valid');
var DeepCompare = require('./deepcompare');

0 comments on commit 84f994a

Please sign in to comment.