Skip to content

Commit

Permalink
Added enhancements (#7)
Browse files Browse the repository at this point in the history
- Support for before_or_equal and after_or_equal
- Support for array format for rules
  • Loading branch information
Ferriel Melarpis authored and ratiw committed Jan 31, 2018
1 parent dde2d94 commit 97050f4
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ and reference it in the script tag. Or, you can use NPMCDN to reference it like
}
var rules = {
name: 'required',
birthday: 'required|date'
// for multiple rules
birthday: 'required|date', // can be a piped string
company: ['required', 'string'] // can be an array of strings
}
var v = Validator.make(data, rules)
Expand Down
46 changes: 45 additions & 1 deletion dist/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ var Validator = function () {
var self = this;
var rules = [];

itemRules.split('|').forEach(function (ruleAndArgs) {
if (typeof itemRules === 'string') {
itemRules = itemRules.split('|');
}

if (!Array.isArray(itemRules)) {
console.error('Unsupported type for item rule', itemRules);
}

itemRules.forEach(function (ruleAndArgs) {
if (ruleAndArgs.trim()) {
var args = ruleAndArgs.split(':');
rules.push({
Expand Down Expand Up @@ -848,6 +856,24 @@ var Validator = function () {

return Date.parse(value) < Date.parse(date);
}
}, {
key: 'validateBeforeOrEqual',
value: function validateBeforeOrEqual(name, value, params) {
this.requireParameterCount(1, params, 'before_or_equal');

if (typeof value !== 'string' && typeof value !== 'number' && !(value instanceof Date)) {
return false;
}

var date = this.hasData(params[0]) ? this.getValue(params[0]) : params[0];

if (!this.validateDate(name, date)) {
console.error(params[0] + ' does not appear to be a date.');
return false;
}

return Date.parse(value) <= Date.parse(date);
}
}, {
key: 'validateAfter',
value: function validateAfter(name, value, params) {
Expand All @@ -866,6 +892,24 @@ var Validator = function () {

return Date.parse(value) > Date.parse(date);
}
}, {
key: 'validateAfterOrEqual',
value: function validateAfterOrEqual(name, value, params) {
this.requireParameterCount(1, params, 'afterOrEqual');

if (typeof value !== 'string' && typeof value !== 'number' && !(value instanceof Date)) {
return false;
}

var date = this.hasData(params[0]) ? this.getValue(params[0]) : params[0];

if (!this.validateDate(name, date)) {
console.error(params[0] + ' does not appear to be a date.');
return false;
}

return Date.parse(value) >= Date.parse(date);
}
}, {
key: 'validateDate',
value: function validateDate(name, value) {
Expand Down
44 changes: 43 additions & 1 deletion src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ export default class Validator {
let self = this
let rules = []

itemRules.split('|').forEach(function(ruleAndArgs) {
if (typeof itemRules === 'string') {
itemRules = itemRules.split('|')
}

if (!Array.isArray(itemRules)) {
console.error('Unsupported type for item rule', itemRules)
}

itemRules.forEach(function(ruleAndArgs) {
if (ruleAndArgs.trim()) {
let args = ruleAndArgs.split(':')
rules.push({
Expand Down Expand Up @@ -701,6 +709,23 @@ export default class Validator {
return (Date.parse(value) < Date.parse(date))
}

validateBeforeOrEqual(name, value, params) {
this.requireParameterCount(1, params, 'before_or_equal')

if (typeof(value) !== 'string' && typeof(value) !== 'number' && !(value instanceof Date)) {
return false
}

let date = this.hasData(params[0]) ? this.getValue(params[0]) : params[0]

if( ! this.validateDate(name, date)) {
console.error(params[0] + ' does not appear to be a date.')
return false
}

return (Date.parse(value) <= Date.parse(date))
}

validateAfter(name, value, params) {
this.requireParameterCount(1, params, 'after')

Expand All @@ -718,6 +743,23 @@ export default class Validator {
return (Date.parse(value) > Date.parse(date))
}

validateAfterOrEqual(name, value, params) {
this.requireParameterCount(1, params, 'afterOrEqual')

if (typeof(value) !== 'string' && typeof(value) !== 'number' && !(value instanceof Date)) {
return false
}

let date = this.hasData(params[0]) ? this.getValue(params[0]) : params[0]

if( ! this.validateDate(name, date)) {
console.error(params[0] + ' does not appear to be a date.')
return false
}

return (Date.parse(value) >= Date.parse(date))
}

validateDate(name, value) {
if (value instanceof Date) {
return true
Expand Down
98 changes: 95 additions & 3 deletions test/Validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ describe('Validator', function() {
describe('#parseItemRules()', function() {
let rules = {
name: 'required|min:3',
group: 'not_in:admin,exec'
group: 'not_in:admin,exec',
nick: ['required', 'string']
}
let v = Validator.make({name: 'Rati'}, rules)
let v = Validator.make({name: 'Rati', nick: 'Rati' }, rules)

it('parses multiple rules correctly', function() {
it('parses multiple piped rules correctly', function() {
let arr = v.parseItemRules(rules['name'])
expect(arr).to.be.lengthOf(2)
expect(arr).to.deep.equal([
{ name: 'Required', params: [] },
{ name: 'Min', params: ['3'] }
])
})
it('parses array of rules correctly', function() {
let arr = v.parseItemRules(rules['nick'])
console.log(arr);
expect(arr).to.be.lengthOf(2)
expect(arr).to.be.deep.equal([
{ name: 'Required', params: [] },
{ name: 'String', params: [] }
])
})
it('parses rule with array argument (not_in)', function() {
let arr = v.parseItemRules(rules['group'])
expect(arr).to.deep.equal([
Expand Down Expand Up @@ -841,6 +851,44 @@ describe('Validator', function() {
expect(v.fails()).to.be.true
})
})
describe('#validateBeforeOrEqual()', function() {
it('returns true when given date is before or equal the specified one', function() {
let v = Validator.make({x: '2000-01-01'}, {x: 'before_or_equal:2012-01-01'})
expect(v.passes()).to.be.true

v = Validator.make({x: '2012-01-01'}, {x: 'before_or_equal:2012-01-01'})
expect(v.passes()).to.be.true

v = Validator.make({x: new Date('2000-01-01')}, {x: 'before_or_equal:2012-01-01'})
expect(v.passes()).to.be.true

v = Validator.make({x: new Date('2012-01-01')}, {x: 'before_or_equal:2012-01-01'})
expect(v.passes()).to.be.true
})
it('returns false when given date is not a string or number', function() {
let v = Validator.make({x: ['2000-01-01']}, {x: 'before_or_equal:2012-01-01'})
expect(v.passes()).to.be.false
})
it('returns true when given date refer to another existing field', function() {
let v = Validator.make({start: '2012-01-01', ends: '2013-01-01'}, {
start: 'before_or_equal:ends',
ends: 'after:start'
})
expect(v.passes()).to.be.true

v = Validator.make({start: '2012-01-01', ends: '2013-01-01'}, {
start: 'before_or_equal:ends',
ends: 'after_or_equal:start'
})
expect(v.passes()).to.be.true

v = Validator.make({start: '2012-01-01', ends: '2000-01-01'}, {
start: 'before_or_equal:ends',
ends: 'after:start'
})
expect(v.fails()).to.be.true
})
})
describe('#validateAfter()', function() {
it('returns true when given date is after the specified one', function() {
let v = Validator.make({x: '2012-01-01'}, {x: 'after:2000-01-01'})
Expand Down Expand Up @@ -876,6 +924,50 @@ describe('Validator', function() {
expect(v.fails()).to.be.true
})
})
describe('#validateAfterOrEqual()', function() {
it('returns true when given date is after or equal the specified one', function() {
let v = Validator.make({x: '2012-01-01'}, {x: 'after_or_equal:2000-01-01'})
expect(v.passes()).to.be.true

v = Validator.make({x: '2000-01-01'}, {x: 'after_or_equal:2000-01-01'})
expect(v.passes()).to.be.true
})
it('returns false when given date is not a string or number', function() {
let v = Validator.make({x: ['2012-01-01']}, {x: 'after_or_equal:2000-01-01'})
expect(v.passes()).to.be.false
})
it('returns true when given date refer to another existing field', function() {
let v = Validator.make({start: '2012-01-01', ends: '2013-01-01'}, {
start: 'after_or_equal:2000-01-01',
ends: 'after_or_equal:start'
})
expect(v.passes()).to.be.true

v = Validator.make({start: '2012-01-01', ends: '2000-01-01'}, {
start: 'after_or_equal:2000-01-01',
ends: 'after_or_equal:start'
})
expect(v.fails()).to.be.true

v = Validator.make({start: new Date('2012-01-01'), ends: '2000-01-01'}, {
start: 'before:ends',
ends: 'after_or_equal:start'
})
expect(v.fails()).to.be.true

v = Validator.make({start: '2012-01-01', ends: new Date('2000-01-01')}, {
start: 'before:ends',
ends: 'after_or_equal:start'
})
expect(v.fails()).to.be.true

v = Validator.make({start: '2012-01-01', ends: '2012-01-01'}, {
start: 'before_or_equal:ends',
ends: 'after_or_equal:start'
})
expect(v.passes()).to.be.true
})
})
describe('#validateArray()', function() {
let rules = { foo: 'array' }
it('returns true when given data is an array', function() {
Expand Down

0 comments on commit 97050f4

Please sign in to comment.