Skip to content

Commit

Permalink
added isBefore, isAfter validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-liu committed Feb 20, 2014
1 parent 7f1da91 commit e7aea10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ v.hasErrors('middle', v.isGender) // ==> ['it is not gender']
* isIn
* minLength
* maxLength
* isUrl
* isBefore
* isAfter
## Progress
The whole framework is done, the only missing is not enough built-in validators. I will add more soon.
23 changes: 20 additions & 3 deletions test/validators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ describe('built-in validators:', function() {
expect(v.hasErrors('aa444433332222', v.isCreditCard)).to.include('it is not credit card number');
expect(v.hasErrors('dfdsafdasfds', v.isCreditCard)).to.include('it is not credit card number');
});
})
});

describe('isUrl', function() {
it('valid url should pass', function() {
expect(v.hasErrors('http://www.google.com', v.isUrl)).to.equal(null);
Expand All @@ -151,6 +152,22 @@ describe('built-in validators:', function() {
expect(v.hasErrors('httpp://www.google.com', v.isUrl)).to.include('it is not url');
//expect(v.hasErrors('http://www .yahoo.com', v.isUrl)).to.include('it is not url');
});
})
});

});
describe('isBefore', function() {
it('valid should pass', function () {
expect(v.hasErrors(new Date(2014,1,21,10,30,0), v.isBefore([new Date(2014,1,21,10,31,0)]))).to.equal(null);
});
it('invalid should pass', function () {
expect(v.hasErrors(new Date(2014,1,21,10,30,0), v.isBefore([new Date(2014,1,21,10,29,0)]))).to.include('it is not before');
});
});

describe('isAfter', function() {
it('valid should pass', function () {
expect(v.hasErrors(new Date(2014,1,21,10,30,0), v.isAfter([new Date(2014,1,21,10,29,0)]))).to.equal(null);
});
it('invalid should pass', function () {
expect(v.hasErrors(new Date(2014,1,21,10,30,0), v.isAfter([new Date(2014,1,21,10,31,0)]))).to.include('it is not after');
});
});});
17 changes: 16 additions & 1 deletion validate-obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@
},
function(name){return m.sprintf('%s is not url', name);}
));

ret.register('isBefore', ret.build(
function(value, params) {
console.log(value);
if(!u.isArray(params) || params.length !==1 || !u.isDate(u.first(params))) throw m.sprintf('isBefore must have one date in the params array');
return value < u.first(params);
},
function(name) {return m.sprintf('%s is not before', name);}
));
ret.register('isAfter', ret.build(
function(value, params) {
console.log(value);
if(!u.isArray(params) || params.length !==1 || !u.isDate(u.first(params))) throw m.sprintf('isAfter must have one date in the params array');
return value > u.first(params);
},
function(name) {return m.sprintf('%s is not after', name);}
));
return ret;
});

0 comments on commit e7aea10

Please sign in to comment.