diff --git a/dist/Validator.js b/dist/Validator.js index 48337d9..1971b9d 100644 --- a/dist/Validator.js +++ b/dist/Validator.js @@ -1 +1 @@ -"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var a=0;a=a[0]&&r<=a[1]}},{key:"validateSize",value:function(e,t,a){return this.requireParameterCount(1,a,"size"),this.getSize(e,t)==a[0]}},{key:"validateBetween",value:function(e,t,a){this.requireParameterCount(2,a,"between");var r=this.getSize(e,t);return r>=a[0]&&r<=a[1]}},{key:"validateMin",value:function(e,t,a){return this.requireParameterCount(1,a,"min"),this.getSize(e,t)>=a[0]}},{key:"validateMax",value:function(e,t,a){return this.requireParameterCount(1,a,"max"),this.getSize(e,t)<=a[0]}},{key:"getSize",value:function(e,t){var a=this.hasRule(e,this.numericRules);return t?a&&!isNaN(parseFloat(t))?parseFloat(t):t.length:0}},{key:"validateIn",value:function(e,t,a){return Array.isArray(t)&&this.hasRule(e,"Array")?0===this.arrayDiff(t,a).length:0<=a.indexOf(t)}},{key:"arrayDiff",value:function(e,t){var a=[];return e.forEach(function(e){t.indexOf(e)<0&&a.push(e)}),a}},{key:"validateNotIn",value:function(e,t,a){return this.requireParameterCount(1,a,"not_in"),!this.validateIn(e,t,a)}},{key:"validateNumeric",value:function(e,t){return this.validateMatch(e,t,/^-?\d+(\.\d*)?$/)}},{key:"validateInteger",value:function(e,t){return this.validateMatch(e,t,/^-?\d+$/)}},{key:"validateString",value:function(e,t){return!this.hasData(e)||(null===t||"string"==typeof t)}},{key:"validateEmail",value:function(e,t){return this.validateMatch(e,t,/^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,8}$/i)}},{key:"validateIp",value:function(e,t){var a=t.split(".");return!!(4===a.length&&this.validateBetween(e,a[0],[1,255])&&this.validateBetween(e,a[1],[0,255])&&this.validateBetween(e,a[2],[0,255])&&this.validateBetween(e,a[3],[1,255]))}},{key:"validateUrl",value:function(e,t){return this.validateMatch(e,t,/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i)}},{key:"validateAlpha",value:function(e,t){return this.validateMatch(e,t,/^([a-z])+$/i)}},{key:"validateAlphaNum",value:function(e,t){return this.validateMatch(e,t,/^([a-z0-9])+$/i)}},{key:"validateAlphaDash",value:function(e,t){return this.validateMatch(e,t,/^([a-z0-9_\-])+$/i)}},{key:"validateBefore",value:function(e,t,a){if(this.requireParameterCount(1,a,"before"),"string"!=typeof t&&"number"!=typeof t&&!(t instanceof Date))return!1;var r=this.hasData(a[0])?this.getValue(a[0]):a[0];return this.validateDate(e,r)?Date.parse(t)Date.parse(r):(console.error(a[0]+" does not appear to be a date."),!1)}},{key:"validateAfterOrEqual",value:function(e,t,a){if(this.requireParameterCount(1,a,"afterOrEqual"),"string"!=typeof t&&"number"!=typeof t&&!(t instanceof Date))return!1;var r=this.hasData(a[0])?this.getValue(a[0]):a[0];return this.validateDate(e,r)?Date.parse(t)>=Date.parse(r):(console.error(a[0]+" does not appear to be a date."),!1)}},{key:"validateDate",value:function(e,t){return t instanceof Date||("string"==typeof t||"number"==typeof t)&&!isNaN(Date.parse(t))}},{key:"validateBoolean",value:function(e,t){if(!this.hasData(e))return!0;return null===t||0<=[!0,!1,0,1,"0","1"].indexOf(t)}},{key:"validateJson",value:function(e,t){try{return JSON.parse(t),!0}catch(e){return!1}}},{key:"strReplace",value:function(e,t,a){if(Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),!Array.isArray(a))for(var r=0;r=a[0]&&r<=a[1]}},{key:"validateSize",value:function(e,t,a){return this.requireParameterCount(1,a,"size"),this.getSize(e,t)==a[0]}},{key:"validateBetween",value:function(e,t,a){this.requireParameterCount(2,a,"between");var r=this.getSize(e,t);return r>=a[0]&&r<=a[1]}},{key:"validateMin",value:function(e,t,a){return this.requireParameterCount(1,a,"min"),this.getSize(e,t)>=a[0]}},{key:"validateMax",value:function(e,t,a){return this.requireParameterCount(1,a,"max"),this.getSize(e,t)<=a[0]}},{key:"getSize",value:function(e,t){var a=this.hasRule(e,this.numericRules);return t?a&&!isNaN(parseFloat(t))?parseFloat(t):t.length:0}},{key:"validateIn",value:function(e,t,a){return Array.isArray(t)&&this.hasRule(e,"Array")?0===this.arrayDiff(t,a).length:0<=a.indexOf(t)}},{key:"arrayDiff",value:function(e,t){var a=[];return e.forEach(function(e){t.indexOf(e)<0&&a.push(e)}),a}},{key:"validateNotIn",value:function(e,t,a){return this.requireParameterCount(1,a,"not_in"),!this.validateIn(e,t,a)}},{key:"validateNumeric",value:function(e,t){return this.validateMatch(e,t,/^-?\d+(\.\d*)?$/)}},{key:"validateInteger",value:function(e,t){return this.validateMatch(e,t,/^-?\d+$/)}},{key:"validateString",value:function(e,t){return!this.hasData(e)||(null===t||"string"==typeof t)}},{key:"validateEmail",value:function(e,t){return this.validateMatch(e,t,/^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,8}$/i)}},{key:"validateIp",value:function(e,t){var a=t.split(".");return!!(4===a.length&&this.validateBetween(e,a[0],[1,255])&&this.validateBetween(e,a[1],[0,255])&&this.validateBetween(e,a[2],[0,255])&&this.validateBetween(e,a[3],[1,255]))}},{key:"validateUrl",value:function(e,t){return this.validateMatch(e,t,/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i)}},{key:"validateAlpha",value:function(e,t){return this.validateMatch(e,t,/^([a-z])+$/i)}},{key:"validateAlphaNum",value:function(e,t){return this.validateMatch(e,t,/^([a-z0-9])+$/i)}},{key:"validateAlphaDash",value:function(e,t){return this.validateMatch(e,t,/^([a-z0-9_\-])+$/i)}},{key:"validateBefore",value:function(e,t,a){if(this.requireParameterCount(1,a,"before"),"string"!=typeof t&&"number"!=typeof t&&!(t instanceof Date))return!1;var r=this.hasData(a[0])?this.getValue(a[0]):a[0];return this.validateDate(e,r)?Date.parse(t)Date.parse(r):(console.error(a[0]+" does not appear to be a date."),!1)}},{key:"validateAfterOrEqual",value:function(e,t,a){if(this.requireParameterCount(1,a,"afterOrEqual"),"string"!=typeof t&&"number"!=typeof t&&!(t instanceof Date))return!1;var r=this.hasData(a[0])?this.getValue(a[0]):a[0];return this.validateDate(e,r)?Date.parse(t)>=Date.parse(r):(console.error(a[0]+" does not appear to be a date."),!1)}},{key:"validateDate",value:function(e,t){return t instanceof Date||("string"==typeof t||"number"==typeof t)&&!isNaN(Date.parse(t))}},{key:"validateBoolean",value:function(e,t){if(!this.hasData(e))return!0;return null===t||0<=[!0,!1,0,1,"0","1"].indexOf(t)}},{key:"validateJson",value:function(e,t){try{return JSON.parse(t),!0}catch(e){return!1}}},{key:"strReplace",value:function(e,t,a){if(Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),!Array.isArray(a))for(var r=0;r { expect(v.fails()).to.be.false; expect(v.invalid()).to.be.empty; }); + + it('should not fail if data is defined as an empty object', () => { + let v = Validator.make({}, { date: 'date' }); + + expect(v.fails()).to.be.false; + }); }); describe('#getErrors()', () => { const rules = { @@ -334,7 +340,7 @@ describe('Validator', () => { describe('#validateRequired()', () => { it('return false when the required field is not present', () => { - const v = Validator.make({}, { name: 'required' }); + const v = Validator.make({ field: 'data' }, { name: 'required' }); expect(v.passes()).to.be.false; }); @@ -827,7 +833,7 @@ describe('Validator', () => { }); it('returns false when the field is not present', () => { - const v = Validator.make({}, rules); + const v = Validator.make({ field: {} }, rules); expect(v.passes()).to.be.false; }); @@ -1435,7 +1441,7 @@ describe('Validator', () => { }); it('returns false when the data is empty (the field under validation is not present)', () => { - const v = Validator.make({}, rules); + const v = Validator.make({ field: 'data' }, rules); expect(v.passes()).to.be.false; }); @@ -1453,7 +1459,7 @@ describe('Validator', () => { f2: 'required_without:f1,f3', f3: 'required_without:f1,f2', }; - const v = Validator.make({}, rules); + const v = Validator.make({ field: 'data' }, rules); expect(v.fails()).to.be.true; v.setData({ f1: 'foo' }, rules); @@ -1487,7 +1493,7 @@ describe('Validator', () => { }; it('returns false when given data is empty', () => { - const v = Validator.make({}, rules); + const v = Validator.make({ field: 'data' }, rules); expect(v.fails()).to.be.true; }); @@ -1608,7 +1614,7 @@ describe('Validator', () => { }); }); - describe('# Error Messages', () => { + describe('#Error Messages', () => { const rules = { name: 'required|min:3', age: 'numeric|min:20', @@ -1649,14 +1655,14 @@ describe('Validator', () => { }); }); - describe('# Others', () => { + describe('#Others', () => { it('tests that empty rules are skipped', () => { const v = Validator.make({ x: 'asksksks' }, { x: '|||required|' }); expect(v.passes()).to.be.true; }); }); - describe('# Custom Names', () => { + describe('#Custom Names', () => { const customNames = { name: 'Name', age: 'Age', @@ -1673,7 +1679,7 @@ describe('Validator', () => { }; it('tests custom name being applied using constructor', () => { - const v = Validator.make({}, rules, {}, customNames); + const v = Validator.make({ field: 'name' }, rules, {}, customNames); expect(v.passes()).to.be.false; expect(v.getErrors()).to.deep.equal(expectedResult); }); @@ -1693,7 +1699,7 @@ describe('Validator', () => { }); }); - describe('# Custom Messages', () => { + describe('#Custom Messages', () => { const rules = { name: 'required', age: 'required', @@ -1737,7 +1743,7 @@ describe('Validator', () => { }); }); - describe('# Displayable values are replaced', () => { + describe('#Displayable values are replaced', () => { it('tests required_if:foo,bar', () => { const v = Validator.make( { color: '1', bar: '' }, @@ -1785,7 +1791,7 @@ describe('Validator', () => { }); }); - describe('# nullable rule tests', () => { + describe('#validateNullable', () => { it('should passed a empty date with "nullable" rule', () => { const v = Validator.make( { diff --git a/yarn.lock b/yarn.lock index ee4d36f..2e3d888 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1382,9 +1382,9 @@ ecc-jsbn@~0.1.1: safer-buffer "^2.1.0" electron-to-chromium@^1.3.488: - version "1.3.509" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.509.tgz#830fcb89cd66dc2984d18d794973b99e3f00584c" - integrity sha512-cN4lkjNRuTG8rtAqTOVgwpecEC2kbKA04PG6YijcKGHK/kD0xLjiqExcAOmLUwtXZRF8cBeam2I0VZcih919Ug== + version "1.3.510" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.510.tgz#dee781ff8b595c0deb60172b75d50b6889757eda" + integrity sha512-sLtGB0znXdmo6lM8hy5wTVo+fLqvIuO8hEpgc0DvPmFZqvBu/WB7AarEwhxVKjf3rVbws/rC8Xf+AlsOb36lJQ== emoji-regex@^7.0.1: version "7.0.3"