Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update readme, add even()/odd(), fix some hard-coded class names

  • Loading branch information...
commit d2872d4c5b0a8c71270b081cc632d841894fecaf 1 parent bdbc4b8
@bronson authored
Showing with 67 additions and 78 deletions.
  1. +53 −74 README.md
  2. +5 −3 lib/valid.js
  3. +1 −1  lib/validjson.js
  4. +8 −0 test/valid.test.js
View
127 README.md
@@ -7,101 +7,80 @@
- Good test coverage.
-# TODO
-
-- test? valid? check? Could these names be more generic?
-
-### Declarative
-
+```javascript
+ var Valid = require('valid');
var inRange = Valid.number().min(4).max(9)
- inRange.verify(12) // throws an error
- var stringInRange = inRange.typeof('string').errorHandler(Valid.TrueFalse)
- if(stringInRange.validate("not in range")) { /\* not executed \*/ }
-
-
-### Immediate
-
- var check = Valid.checker(TrueFalse); // uses Throw by default
- if(check(12).inRange(4,9)) { /\* not executed \*/ }
-
-what about going insane?
-
- Object.prototype.assert = Valid.checker().errorHandler(Valid.Throw);
- 12.assert().min(4).max(9); // throws an error
-
+ 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"
-# Handling Validation Errors
+ 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
-By default the validator raises an error when it finds invalid data
-(using the Valid.Throw handler). To make it return a boolean result:
+ // validate JSON schemas:
+ var Valid = require('validjson');
+ Valid.json({a:1,b:Valid.integer()}).verify({a:1,b:10});
- Valid.errorHandler(Valid.TrueFalse).validate(false);
+ TODO? Valid(9).lt(12).gt(10) // immediate mode, throws "9 is not greater than 10"
+```
-### Throw
+# Gruntles
-### Console
-
-### TrueFalse
-
-### ErrorObject (included with json-valid)
-
- errors: an error object
- .add([field], msg) -- adds a message for the field. A field of "." refers to the entire object.
- .clear() -- clears all error messages
- .count([field]) -- tells how many total errors or just for the given field
- .messages([field]) -- returns an array of all error messages concated with field name
+- test? valid? check? Could these names be more generic?
+- todo: isDate, isBefore, isAfter
+- todo: isEmail() isIP() isUrl() isUUID()
+- noexisty is a stupid name
# Built-In Validations
-Don't include validations by default?
- Syntax is very personal. Do you prefer in, contains, isIn, etc?
-Don't do isBlah. Just blah.
-Find the BARE MINIMUM non-composed validators. Users can compose their own APIs.
-
+Examples:
-in(1,2) equivalent: in([1,2]) in({1:ignored,2:ignored})
- in is a keyword... use synonym isIn if this is a problem.
- have a synonym module? Valid.isOneOf = Valid.in
+- Valid.equal(null).verify(null)
+- Valid.equal(1,2,3).verify(2)
+- Valid.len(3,5).verify("1234"); // Valid.length(3,5) works too but some JS implementations complain
+- Valid.len(3,5).verify("[1,2,3,4]");
-match(regex)
-length(min, [max]) applies to strings, arrays, or any object that has a length field.
+A compendium:
-and(Validation, Validation, ...)
- though why would you do this since Validation.validation().validation() is the same.
-or(Validation, Validation, ...)
-not(Validation)
- Make any a synonym for or and every a synonym or and?
- What about not(and) and not(or)?
-optional(Validation, Validation, ...)
-min -- uses JS's < and > operators so can be used on strings too.
-max --
- also gt, ge, lt, le, eq, ne?
+- equal(a[,b...]), notEqual(...), oneOf(arrayOrObject)
+- defined(), undef(), undefined()
+- nil(), null(), notNull()
+- exists(), noexisty() (!)
+- array([test]), len(min,max), empty()
+- boolean(), true(), false()
+- number(), integer(), mod(x[,rem]), even(), odd(), max(n), min(n)
+- string([test]), len(min,max), blank(), notBlank()
+- match(regex[,modifiers]), nomatch(regex[,modifiers])
+- eq(n), lt(n), le(n), ge(n), gt(n), ne(n)
+- nop(), fail([message]), messageFor(test,message), todo([test])
+- and(test[,test...]), or(test[,test...]), not(test,message)
-isDate
- isBefore
- isAfter
-
-isEmail()
-isIP()
-isUrl()
-isUUID()
-
-json(template) -- checks an entire json structure
+# Extending Valid
+To define your own tests, just end the chain with "define()"
+and add it to the root object:
-We don't offer inverses because not() is usually clearer. If you'd
-rather write Valid.notIn(arr) instead of Valid.not(Valid.in(arr)),
-just do this:
+```javascript
+ Valid.latitude = Valid.ge(-90).le(90).define();
+ Valid.longitude = Valid.ge(-180).le(180).define();
+ Valid.integer().latitude().verify(20); // success!
+```
- Valid.notIn = function(arr) { return Valid.not(Valid.in(arr)) }
- Valid.notIn = Valid.not(Valid.in) // could this work?
+You can also add tests 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"
+```
-# Extending Valid
+Or just rename them:
-## Validators
+```javascript
+ Valid.every = Valid.and;
+ Valid.any = Valid.or;
+```
# Alternatives
View
8 lib/valid.js
@@ -36,7 +36,7 @@ Valid.equal = Valid.SimpleTest(function Equal(value) {
var opts = [];
for(var i=1; i<arguments.length; i++) {
if(value === arguments[i]) return;
- opts.push(Valid.Escape(arguments[i]));
+ opts.push(this.Escape(arguments[i]));
}
if(arguments.length === 2) return "is not equal to " + opts[0];
var lastopt = opts.pop();
@@ -129,6 +129,8 @@ Valid.empty = Valid.messageFor(Valid.optional().len(0,0), "is not empty").d
Valid.boolean = Valid.type('boolean').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.string = Valid.type('string').define();
Valid.blank = Valid.messageFor(Valid.optional().match(/^\s*$/), "is not blank").define();
Valid.notBlank = Valid.not(Valid.blank(), "is blank").define();
@@ -144,8 +146,8 @@ Valid['in'] = Valid.oneOf;
Valid.length = Valid.len;
Valid.todo = function(name) { return this.fail((name ? name : "this") + " is still todo"); };
-Valid.notEqual = function(arg) { return Valid.not(Valid.equal(arg), "is equal to " + Valid.Escape(arg)); };
-Valid.nomatch = function(pat,mods) { var match = Valid.match(pat,mods); return Valid.not(match, "matches " + match._queue[1].data); };
+Valid.notEqual = function(arg) { return this.not(this.equal(arg), "is equal to " + this.Escape(arg)); };
+Valid.nomatch = function(pat,mods) { var match = this.match(pat,mods); return this.not(match, "matches " + match._queue[1].data); };
Valid.eq = Valid.equal;
Valid.ne = Valid.notEqual;
View
2  lib/validjson.js
@@ -43,7 +43,7 @@ Valid.JsonField = function(path, value, schema) {
case 'number':
case 'boolean':
case 'undefined':
- if(value !== schema) this.JsonError(path, value, "does not equal " + Valid.Escape(schema));
+ if(value !== schema) this.JsonError(path, value, "does not equal " + this.Escape(schema));
break;
case 'function':
View
8 test/valid.test.js
@@ -98,6 +98,14 @@ 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");
+Valid.even().assert(0);
+Valid.even().assert(12);
+Valid.even().assert(17, "is not even");
+Valid.even().assert(0);
+Valid.odd().assert(1);
+Valid.odd().assert(17);
+Valid.odd().assert(12, "is not odd");
+Valid.odd().assert(0, "is not odd");
// strings
Valid.string().assert(' 123');
Please sign in to comment.
Something went wrong with that request. Please try again.