Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fea1507
Showing
7 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
.DS_Store | ||
node_modules | ||
run.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
run.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
[propex-validation](http://williamwicks.github.com/propex-validation) is a small framework for validating javascript objects against a [propex](http://williamwicks.github.com/propex) and a Validator object. | ||
|
||
|
||
##The Validator object | ||
```javascript | ||
var validator = new Validator({ | ||
name: { | ||
test: function(value){ if(value.length>30) return "Too long";}, | ||
set: function(model, value){ | ||
model.name = value + " is awesome"; | ||
} | ||
}, | ||
locations: new Validator({ | ||
storeid: { | ||
test: function(value){ if(isNaN(value)) return "Numeric Id needed"; } | ||
}, | ||
position: { | ||
parse: function(propex, data){ return data.lng+","+data.lat }, | ||
test: function(value){ if(/^\d\d,\d\d$/.test(value)) return "Lng & Lat cannot be over 99"; } | ||
}, | ||
departments: { | ||
missing: function() { return "You're going to need to provide these"; }, | ||
parse: new Validator({ | ||
name: { test: length(3,32) }, | ||
phone: { test: function(value) { if(!/^\d\d\d-?\d\d\d-?\d\d\d\d$/.test(value)) return "Bad phone number"; } } | ||
}), | ||
test: function(value) { if(value.length<2) return "You're going to need to provide more"; }, | ||
set: function(model, value) { model.departments = value; } | ||
} | ||
}) | ||
}); | ||
``` | ||
|
||
##Example validation | ||
```javascript | ||
var result = validator("{StoreName,Departments[0{Name,Phone,Hours},1{Name,Phone?,Hours?}]1:5?}",{ | ||
StoreName:"Walmart", | ||
Departments: [ | ||
{Name:"Main", Phone:"1231231234", Hours: "9-5"}, | ||
{Name:"Pharmacy", Phone:"1235551212"} | ||
] | ||
}) | ||
``` | ||
|
||
# Installation | ||
|
||
$ npm install propex-validation | ||
|
||
## License | ||
|
||
The MIT License (MIT) | ||
Copyright (c) 2012 William Wicks | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
|
||
module.exports = { | ||
validator: require("./validator") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "propex-validation", | ||
"version": "0.0.1", | ||
"description": "Uses Propex to validate javascript objects", | ||
"keywords": ["propex", "property expressions", "json validation"], | ||
"homepage": "http://github.com/williamwicks/propex-validation", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:williamwicks/propex-validation.git" | ||
}, | ||
"author" : { | ||
"name" : "William Wicks", | ||
"email" : "wjwicks@gmail.com" | ||
}, | ||
"dependencies": { | ||
"propex":"0.1.3" | ||
}, | ||
"devDependencies": { | ||
"vows":"*" | ||
}, | ||
"main": "index", | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"licenses": [{"type": "MIT", "url": "http://www.opensource.org/licenses/MIT"}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
|
||
|
||
var vows = require('vows'), | ||
assert = require('assert'), | ||
Validator = require("./validator"); | ||
|
||
function length(min, max) { return function(value){ | ||
if(value.length<min) return "Too short"; | ||
if(value.length>max) return "Too long"; | ||
}} | ||
|
||
|
||
|
||
|
||
|
||
vows.describe("Validation tests") | ||
.addBatch({ | ||
"Constructing a Validator without any params": { | ||
topic: function(){ | ||
return new Validator() | ||
}, | ||
"is pointless- but doable": function(v){ | ||
assert.isNotNull(v); | ||
assert.isFunction(v); | ||
} | ||
} | ||
}) | ||
.addBatch({ | ||
'Validator for {name:"ACME"}': { | ||
topic: function(){ | ||
return new Validator({ | ||
name: { test: function(value) { if(value != "roojoo") return "Booooo. Bad kitty." } } | ||
}); | ||
}, | ||
"Propex:[]": { | ||
"Data:{}": { | ||
topic: function(validate){ | ||
return validate("[]", {}); | ||
}, | ||
"errors should equal the default 'required' message": function(result){ | ||
assert.equal(result.errors, Validator.errors.required()); | ||
} | ||
}, | ||
"Data:[]": { | ||
topic: function(validate){ | ||
return validate("[]", []); | ||
}, | ||
"should not return errors and valid should be empty": function(result){ | ||
assert.isUndefined(result.error); | ||
assert.isArray(result.valid); | ||
assert.isEmpty(result.valid); | ||
} | ||
}, | ||
"Data:[1,2,3]": { | ||
topic: function(validate){ | ||
return validate("[]", [1,2,3]); | ||
}, | ||
"throws error: needs to have subitems specified in propex": function(result){ | ||
assert.instanceOf(result, Error); | ||
} | ||
}, | ||
"noop": {} | ||
}, | ||
"Propex:{}": { | ||
"Data:{}": { | ||
topic: function(validate){ | ||
return validate("{}", {}); | ||
}, | ||
"should not return errors and valid should be empty": function(result){ | ||
assert.isUndefined(result.error); | ||
assert.isObject(result.valid); | ||
assert.isEmpty(result.valid); | ||
} | ||
}, | ||
"Data:[]": { | ||
topic: function(validate){ | ||
return validate("{}", []); | ||
}, | ||
"errors should equal the default 'required' message": function(result){ | ||
assert.equal(result.errors, Validator.errors.required()); | ||
} | ||
}, | ||
'Data:{x:1,name:"roojoo"}': { | ||
topic: function(validate){ | ||
return validate("{}", {x:1}); | ||
}, | ||
"should ignore properties that are not in the propex": function(result){ | ||
assert.isEmpty(result.valid); | ||
assert.isUndefined(result.errors); | ||
} | ||
}, | ||
"bookend": {} | ||
}, | ||
'Propex:{name,type}': { | ||
'Data:{name:"lace",type:"cat"}': { | ||
topic: function(validate){ | ||
return validate('{name,type}', {name:"lace",type:"cat",xtra:"ignored"}); | ||
}, | ||
"should have an error because the name does not match 'roojoo'": function(result){ | ||
assert.isObject(result.errors); | ||
assert.includes(result.errors, 'name'); | ||
} | ||
}, | ||
'Data:{name:"roojoo",type:"cat",xtra:"ignored"}': { | ||
topic: function(validate){ | ||
return validate('{name,type}', {name:"roojoo",type:"cat",xtra:"ignored"}); | ||
}, | ||
"should successfully find all required items and ignore xtra": function(result){ | ||
assert.isUndefined(result.errors); | ||
assert.isObject(result.valid); | ||
assert.equal(result.valid.name, 'roojoo'); | ||
assert.equal(result.valid.type, 'cat'); | ||
assert.isUndefined(result.valid.xtra); | ||
} | ||
}, | ||
"bookend": {} | ||
} | ||
} | ||
}) | ||
.run(); | ||
|
||
|
||
|
||
/* | ||
AssertionError | ||
fail | ||
ok | ||
equal | ||
notEqual | ||
deepEqual | ||
notDeepEqual | ||
strictEqual | ||
notStrictEqual | ||
throws | ||
doesNotThrow | ||
ifError | ||
match | ||
matches | ||
isTrue | ||
isFalse | ||
isZero | ||
isNotZero | ||
greater | ||
lesser | ||
inDelta | ||
include | ||
includes | ||
deepInclude | ||
deepIncludes | ||
isEmpty | ||
isNotEmpty | ||
lengthOf | ||
isArray | ||
isObject | ||
isNumber | ||
isBoolean | ||
isNaN | ||
isNull | ||
isNotNull | ||
isUndefined | ||
isDefined | ||
isString | ||
isFunction | ||
typeOf | ||
instanceOf | ||
*/ |
Oops, something went wrong.