Skip to content

Commit

Permalink
Corrected prototype and constructor assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
williamkapke committed Jul 16, 2012
1 parent 0ce87e0 commit 9f212c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
31 changes: 19 additions & 12 deletions tests.js
Expand Up @@ -14,13 +14,26 @@ function length(min, max) { return function(value){

vows.describe("Validation tests")
.addBatch({
"Constructing a Validator without any params": {
topic: function(){
return new Validator()
"Constructing a Validator": {
"without any params": {
topic: function(){
return new Validator()
},
"is pointless- but doable": function(v){
assert.isNotNull(v);
assert.equal(typeof v, 'function');
}
},
"is pointless- but doable": function(v){
assert.isNotNull(v);
assert.isFunction(v);
"with a sub Validator": {
topic: function(){
return new Validator({
people: new Validator()
})
},
"converted the Validator object to {parse: ... }": function(v){
assert.includes(v.validators, 'people');
assert.includes(v.validators.people,'parse');
}
}
}
})
Expand Down Expand Up @@ -93,22 +106,18 @@ vows.describe("Validation tests")
'Propex:{name,type}': {
'Data:{name:"lace",type:"cat"}': {
topic: function(validate){
debugger;
return validate('{name,type}', {name:"lace",type:"cat",xtra:"ignored"});
},
"should have an error because the name does not match 'roojoo'": function(result){
debugger;
assert.isObject(result.errors);
assert.includes(result.errors, 'name');
}
},
'Data:{name:"roojoo",type:"cat",xtra:"ignored"}': {
topic: function(validate){
debugger;
return validate('{name,type}', {name:"roojoo",type:"cat",xtra:"ignored"});
},
"should successfully find all required items and ignore xtra": function(result){
debugger;
assert.isUndefined(result.errors);
assert.isObject(result.valid);
assert.equal(result.valid.name, 'roojoo');
Expand Down Expand Up @@ -146,11 +155,9 @@ vows.describe("Validation tests")
},
"valid name":{
topic: function(validate){
debugger;
return validate("{name}", {name:"Nicole"});
},
"calls 'set' and appends to the end": function(result){
debugger;
assert.isUndefined(result.errors);
assert.include(result.valid, 'name');
assert.equal(result.valid.name, "Nicole is a suitable name.");
Expand Down
26 changes: 14 additions & 12 deletions validator.js
Expand Up @@ -2,17 +2,7 @@
var Px = require("propex");

function Validator(validators){
validators = (function(temp){
Object.keys(validators || {}).forEach(function(k) {
var v = validators[k];
if(v.prototype == Validator){
temp[k] = { parse: v };
}
else temp[k] = clone(v);
});
return temp;
})({});


function fn(propex, value, augmentors){
if(typeof propex == "string")
propex = Px(propex);
Expand All @@ -31,7 +21,19 @@ function Validator(validators){

return result;
}
fn.prototype = Validator;
fn.constructor = Validator;
fn.__proto__ = Validator.prototype;
fn.validators = (function(temp){
Object.keys(validators || {}).forEach(function(k) {
var v = validators[k];
if(v.constructor == Validator){
temp[k] = { parse: v };
}
else temp[k] = clone(v);
});
return temp;
})({});

return fn;

//----------------------------------------------------
Expand Down

0 comments on commit 9f212c8

Please sign in to comment.