From 741c0be9857ec53c820787c66bd50284a0d0fcb8 Mon Sep 17 00:00:00 2001 From: paulohrodriguesaifal Date: Sun, 18 Mar 2018 22:13:56 -0300 Subject: [PATCH 1/4] add file validation with functions extensionFile and maxSizeFile --- README.md | 3 +++ dist/validate.js | 2 +- docs/file.md | 46 ++++++++++++++++++++++++++++++++++++++++++ src/file.js | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/lib/file.js | 1 + src/validate.js | 3 +++ test/file.test.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 docs/file.md create mode 100644 src/file.js create mode 100644 src/lib/file.js create mode 100644 test/file.test.js diff --git a/README.md b/README.md index 15e82f6..94a4606 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,9 @@ Also available for [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) - [x] isDiners - [x] isAmex - [x] isCreditCard +- [x] [File](https://github.com/dleitee/valid.js/blob/master/docs/file.md) + - [x] extensionFile + - [x] maxSizeFile # Contributing diff --git a/dist/validate.js b/dist/validate.js index c2fb9c2..99f3a51 100644 --- a/dist/validate.js +++ b/dist/validate.js @@ -1 +1 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,(r.valid||(r.valid={})).js=e()}}(function(){return function e(r,t,n){function i(o,a){if(!t[o]){if(!r[o]){var f="function"==typeof require&&require;if(!a&&f)return f(o,!0);if(u)return u(o,!0);var d=new Error("Cannot find module '"+o+"'");throw d.code="MODULE_NOT_FOUND",d}var c=t[o]={exports:{}};r[o][0].call(c.exports,function(e){var t=r[o][1][e];return i(t?t:e)},c,c.exports,e,r,t,n)}return t[o].exports}for(var u="function"==typeof require&&require,o=0;o=r}},o=function(){var e=arguments.length<=0||void 0===arguments[0]?new Date(1900,1,1):arguments[0];return function(r){return(0,n._isDate)(r)&&r>=e}},a=function(){var e=arguments.length<=0||void 0===arguments[0]?new Date(1900,1,1):arguments[0],r=arguments.length<=1||void 0===arguments[1]?new Date(3e3,1,1):arguments[1];return function(t){return o(e)(t)&&u(r)(t)}};t.date={maxDate:u,minDate:o,between:a,isDate:i}},{"./lib/date":6}],4:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(e){var r=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return r.test(e)},i=function(e){var r=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;return r.test(e)},u=function(e){return n(e)||i(e)};t.ip={isIp:u,isIpv4:n,isIpv6:i}},{}],5:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t._isArray=function(e){return null!==e&&e.length>=0&&"[object Array]"===Object.prototype.toString.call(e)}},{}],6:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t._isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)}},{}],7:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.isNil=function(e){return null===e||void 0===e}},{}],8:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t._isNumber=function(e){return"[object Number]"===Object.prototype.toString.call(e)}},{}],9:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t._isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)}},{}],10:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.reduce=function(e,r,t){if(Array.prototype.reduce)return Array.prototype.reduce.apply(t,[e,r]);var n=t[Symbol.iterator](),i=function u(r,t){var n=t.next(),i=n.value,o=n.done;return o?r:u(e(r,i),t)};return i(r,n)}},{}],11:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t._isString=function(e){return"[object String]"===Object.prototype.toString.call(e)}},{}],12:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.number=void 0;var n=e("./lib/number"),i=function(e){return(0,n._isNumber)(e)},u=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MAX_VALUE:arguments[0];return function(r){return(0,n._isNumber)(r)&&e>=r}},o=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MIN_VALUE:arguments[0];return function(r){return(0,n._isNumber)(r)&&r>=e}},a=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MIN_VALUE:arguments[0],r=arguments.length<=1||void 0===arguments[1]?Number.MAX_VALUE:arguments[1];return function(t){return o(e)(t)&&u(r)(t)}};t.number={maxNumber:u,minNumber:o,between:a,isNumber:i}},{"./lib/number":8}],13:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.string=void 0;var n=e("./lib/isnil.js"),i=e("./lib/string"),u=function(e){return(0,i._isString)(e)},o=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MIN_VALUE:arguments[0];return function(r){return(0,i._isString)(r)&&r.length>=e}},a=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MAX_VALUE:arguments[0];return(0,n.isNil)(e)&&(e=Number.MAX_VALUE),function(r){return(0,i._isString)(r)&&r.length<=e}},f=function(){var e=arguments.length<=0||void 0===arguments[0]?Number.MIN_VALUE:arguments[0],r=arguments.length<=1||void 0===arguments[1]?Number.MAX_VALUE:arguments[1];return function(t){return o(e)(t)&&a(r)(t)}},d=function(e){return function(r){var t=new RegExp(e);return t.test(r)}};t.string={isString:u,minLength:o,maxLength:a,length:f,regex:d}},{"./lib/isnil.js":7,"./lib/string":11}],14:[function(e,r,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.util=void 0;var n=e("./lib/isnil.js"),i=e("./lib/array.js"),u=e("./lib/number.js"),o=e("./lib/object.js"),a=e("./lib/string.js"),f=e("./string.js"),d=function(e){return(0,n.isNil)(e)?!1:(0,i._isArray)(e)?e.length>0:(0,u._isNumber)(e)?e>0:(0,o._isObject)(e)?Object.keys(e).length>0:(0,a._isString)(e)?e.length>0:Boolean(e)},c=function(e){var r="[^\\x0d\\x22\\x5c\\x80-\\xff]",t="[^\\x0d\\x5b-\\x5d\\x80-\\xff]",n="[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+",i="\\x5c[\\x00-\\x7f]",u="\\x5b("+t+"|"+i+")*\\x5d",o="\\x22("+r+"|"+i+")*\\x22",a=n,d="("+a+"|"+u+")",c="("+n+"|"+o+")",s=d+"(\\x2e"+d+")*",l=c+"(\\x2e"+c+")*",b=l+"\\x40"+s,v="^"+b+"$";return f.string.regex(v)(e)},s=function(e){var r=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;return f.string.regex(r)(e)},l=function(e){return f.string.regex(/^[0-9]{5}-[0-9]{3}$/)(e)},b=function(e){return f.string.regex(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/)(e)},v=function(e){var r=/^(\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2})$/;if(r.test(e)){var t=e.replace(/\.|-/g,"");if("00000000000"===t||"11111111111"===t||"22222222222"===t||"33333333333"===t||"44444444444"===t||"55555555555"===t||"66666666666"===t||"77777777777"===t||"88888888888"===t||"99999999999"===t)return!1;for(var n=void 0,i=0,u=0;9>u;u++)i+=parseInt(t.charAt(u),10)*(10-u);if(n=11-i%11,10!==n&&11!==n||(n=0),n!==parseInt(t.charAt(9),10))return!1;i=0;for(var o=0;10>o;o++)i+=parseInt(t.charAt(o),10)*(11-o);return n=11-i%11,10!==n&&11!==n||(n=0),n===parseInt(t.charAt(10),10)}return!1},p=function(e){var r=/^(\d{2}[\.]?\d{3}[\.]?\d{3}[\/]?\d{4}[-]?\d{2})$/;if(r.test(e)){var t=e.replace(/\.|-|\//g,"");if("00000000000000"===t||"11111111111111"===t||"22222222222222"===t||"33333333333333"===t||"44444444444444"===t||"55555555555555"===t||"66666666666666"===t||"77777777777777"===t||"88888888888888"===t||"99999999999999"===t)return!1;for(var n=0,i=12,u=t.substring(0,i),o=t.substring(i),a=i-7,f=i;f>=1;f--)n+=u.charAt(i-f)*a--,2>a&&(a=9);var d=2>n%11?0:11-n%11;if(d!==parseInt(o.charAt(0),10))return!1;i+=1,u=t.substring(0,i),n=0,a=i-7;for(var c=i;c>=1;c--)n+=u.charAt(i-c)*a--,2>a&&(a=9);return d=2>n%11?0:11-n%11,d===parseInt(o.charAt(1),10)}return!1};t.util={isRequired:d,isEmail:c,isCep:l,isURL:s,isHexColor:b,isCpf:v,isCnpj:p}},{"./lib/array.js":5,"./lib/isnil.js":7,"./lib/number.js":8,"./lib/object.js":9,"./lib/string.js":11,"./string.js":13}],15:[function(e,r,t){"use strict";function n(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);rt;t++)r[t]=arguments[t];return function(e){return(0,s.isNil)(e)?!1:(0,b.reduce)(function(r,t){if((0,s.isNil)(t))throw new Error("The function for validation is null or undefined.");return r&&t(e)},!0,r)}};t.isValid=function(e){var r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];return v.apply(void 0,n((0,l._isArray)(r)?r:[r]))(e)}},{"./boolean":1,"./creditCard":2,"./date":3,"./ip":4,"./lib/array.js":5,"./lib/isnil.js":7,"./lib/reduce.js":10,"./number":12,"./string":13,"./util":14}]},{},[15])(15)}); \ No newline at end of file +!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,(t.valid||(t.valid={})).js=e()}}(function(){return function e(t,r,n){function i(o,a){if(!r[o]){if(!t[o]){var f="function"==typeof require&&require;if(!a&&f)return f(o,!0);if(u)return u(o,!0);var d=new Error("Cannot find module '"+o+"'");throw d.code="MODULE_NOT_FOUND",d}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){var r=t[o][1][e];return i(r?r:e)},c,c.exports,e,t,r,n)}return r[o].exports}for(var u="function"==typeof require&&require,o=0;o0&&void 0!==arguments[0]?arguments[0]:new Date(3e3,1,1);return function(t){return(0,n._isDate)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1);return function(t){return(0,n._isDate)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1),t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date(3e3,1,1);return function(r){return o(e)(r)&&u(t)(r)}};r.date={maxDate:u,minDate:o,between:a,isDate:i}},{"./lib/date":7}],4:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.file=void 0;var n=e("./lib/string"),i=e("./lib/file"),u=e("./lib/number"),o=function(e){return function(t){if(t=(0,n._isString)(t)?t:!!(0,i._isFile)(t)&&t.name,t===!1)return!1;var r=t.substring(t.lastIndexOf(".")+1);return e.indexOf(r)>=0}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){return!!(0,u._isNumber)(e)&&(!!(0,i._isFile)(t)&&!(t.size/1024>e))}};r.file={extensionFile:o,maxSizeFile:a}},{"./lib/file":8,"./lib/number":10,"./lib/string":13}],5:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=function(e){var t=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return t.test(e)},i=function(e){var t=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;return t.test(e)},u=function(e){return n(e)||i(e)};r.ip={isIp:u,isIpv4:n,isIpv6:i}},{}],6:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isArray=function(e){return null!==e&&e.length>=0&&"[object Array]"===Object.prototype.toString.call(e)}},{}],7:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)}},{}],8:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isFile=function(e){return"[object File]"===Object.prototype.toString.call(e)||"[object Object]"===Object.prototype.toString.call(e)&&null!=e.size&&null!=e.name}},{}],9:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.isNil=function(e){return null===e||void 0===e}},{}],10:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isNumber=function(e){return"[object Number]"===Object.prototype.toString.call(e)}},{}],11:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)}},{}],12:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.reduce=function(e,t,r){if(Array.prototype.reduce)return Array.prototype.reduce.apply(r,[e,t]);var n=r[Symbol.iterator](),i=function u(t,r){var n=r.next(),i=n.value,o=n.done;return o?t:u(e(t,i),r)};return i(t,n)}},{}],13:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isString=function(e){return"[object String]"===Object.prototype.toString.call(e)}},{}],14:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.number=void 0;var n=e("./lib/number"),i=function(e){return(0,n._isNumber)(e)},u=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){return(0,n._isNumber)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,n._isNumber)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&u(t)(r)}};r.number={maxNumber:u,minNumber:o,between:a,isNumber:i}},{"./lib/number":10}],15:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.string=void 0;var n=e("./lib/isnil.js"),i=e("./lib/string"),u=function(e){return(0,i._isString)(e)},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,i._isString)(t)&&t.length>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return(0,n.isNil)(e)&&(e=Number.MAX_VALUE),function(t){return(0,i._isString)(t)&&t.length<=e}},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&a(t)(r)}},d=function(e){return function(t){var r=new RegExp(e);return r.test(t)}};r.string={isString:u,minLength:o,maxLength:a,length:f,regex:d}},{"./lib/isnil.js":9,"./lib/string":13}],16:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.util=void 0;var n=e("./lib/array.js"),i=e("./lib/number.js"),u=e("./lib/object.js"),o=e("./lib/string.js"),a=e("./string.js"),f=function(e){return(0,n._isArray)(e)?e.length>0:(0,i._isNumber)(e)?e>0:(0,u._isObject)(e)?Object.keys(e).length>0:(0,o._isString)(e)?e.length>0:Boolean(e)},d=function(e){var t="[^\\x0d\\x22\\x5c]",r="[^\\x0d\\x5b-\\x5d]",n="[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d]+",i="\\x5c[\\x00-\\x7f]",u="\\x5b("+r+"|"+i+")*\\x5d",o="\\x22("+t+"|"+i+")*\\x22",f=n,d="("+f+"|"+u+")",c="("+n+"|"+o+")",s=d+"(\\x2e"+d+")*",l=c+"(\\x2e"+c+")*",b=l+"\\x40"+s,v="^"+b+"$";return a.string.regex(v)(e)},c=function(e){var t=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;return a.string.regex(t)(e)},s=function(e){return a.string.regex(/^[0-9]{5}-[0-9]{3}$/)(e)},l=function(e){return a.string.regex(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/)(e)},b=function(e){var t=/^(\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-/g,"");if("00000000000"===r||"11111111111"===r||"22222222222"===r||"33333333333"===r||"44444444444"===r||"55555555555"===r||"66666666666"===r||"77777777777"===r||"88888888888"===r||"99999999999"===r)return!1;for(var n=void 0,i=0,u=0;u<9;u++)i+=parseInt(r.charAt(u),10)*(10-u);if(n=11-i%11,10!==n&&11!==n||(n=0),n!==parseInt(r.charAt(9),10))return!1;i=0;for(var o=0;o<10;o++)i+=parseInt(r.charAt(o),10)*(11-o);return n=11-i%11,10!==n&&11!==n||(n=0),n===parseInt(r.charAt(10),10)}return!1},v=function(e){var t=/^(\d{2}[\.]?\d{3}[\.]?\d{3}[\/]?\d{4}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-|\//g,"");if("00000000000000"===r||"11111111111111"===r||"22222222222222"===r||"33333333333333"===r||"44444444444444"===r||"55555555555555"===r||"66666666666666"===r||"77777777777777"===r||"88888888888888"===r||"99999999999999"===r)return!1;for(var n=0,i=12,u=r.substring(0,i),o=r.substring(i),a=i-7,f=i;f>=1;f--)n+=u.charAt(i-f)*a--,a<2&&(a=9);var d=n%11<2?0:11-n%11;if(d!==parseInt(o.charAt(0),10))return!1;i+=1,u=r.substring(0,i),n=0,a=i-7;for(var c=i;c>=1;c--)n+=u.charAt(i-c)*a--,a<2&&(a=9);return d=n%11<2?0:11-n%11,d===parseInt(o.charAt(1),10)}return!1};r.util={isRequired:f,isEmail:d,isCep:s,isURL:c,isHexColor:l,isCpf:b,isCnpj:v}},{"./lib/array.js":6,"./lib/number.js":10,"./lib/object.js":11,"./lib/string.js":13,"./string.js":15}],17:[function(e,t,r){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t1&&void 0!==arguments[1]?arguments[1]:[];return _.apply(void 0,n((0,b._isArray)(t)?t:[t]))(e)}},{"./boolean":1,"./creditCard":2,"./date":3,"./file":4,"./ip":5,"./lib/array.js":6,"./lib/isnil.js":9,"./lib/reduce.js":12,"./number":14,"./string":15,"./util":16}]},{},[17])(17)}); \ No newline at end of file diff --git a/docs/file.md b/docs/file.md new file mode 100644 index 0000000..c8f4dfc --- /dev/null +++ b/docs/file.md @@ -0,0 +1,46 @@ +# Files + +```es6 +import {file} from 'valid.js' +``` + +## extensionFile(arrayFileExtensions)(file) + +Validates that 'fileExtension' belongs to a specific set of extensions + +#### Usage + +```es6 +import {validate, file} from 'valid.js' + +//fileValue type object Object or object File + +let fileValue = document.getElementById("file").files[0] // selected a image .jpg +let isvalid = validate(file.extensionFile(["jpg","png","pdf"])) +let result = isvalid(fileValue) +// result => true + +fileValue = document.getElementById("file").files[0] // selected a file .odt +let resultFalse = isvalid(fileValue) +// resultFalse => false +``` + +## maxSizeFile(maxValue)(file) + +Validates the size of the 'file' is less than or equal to (<=) the value + +#### Usage +```es6 +import {validate, file} from 'valid.js' + +let fileValue = document.getElementById("file").files[0] // selected a file of 1024 bytes +let isvalid = validate(file.maxSizeFile(1)) //value in Kilobyte +let result = isvalid(fileValue) +// result => true + +fileValue = document.getElementById("file").files[0] // selected a file of 1025 bytes +let resultFalse = isvalid(1) +// resultFalse => false +``` + +. \ No newline at end of file diff --git a/src/file.js b/src/file.js new file mode 100644 index 0000000..8f3e5c1 --- /dev/null +++ b/src/file.js @@ -0,0 +1,48 @@ +import {_isString} from './lib/string'; +import {_isFile} from './lib/file'; +import {_isNumber} from './lib/number'; + +/* + * Check file extension + * @params exts + * @params value + * @return boolean + */ + +const extensionFile = exts => { + return (value=>{ + value = _isString(value) ? value: ( + _isFile(value) ? value.name : false + ); + if(value===false) return false; + let ext = value.substring(value.lastIndexOf('.') + 1); + return ((exts.indexOf(ext)>=0) ? true: false); + }); +}; + +/* + * Check size of file in megabytes + * @params maxSizeFile + * @params file + * @return boolean + */ + +const maxSizeFile = (maxSizeFileValue = Number.MAX_VALUE) => { + return (file=>{ + if(!_isNumber(maxSizeFileValue)){ + return false; + } + if(_isFile(file)){ + return ((file.size/1024) > maxSizeFileValue) ? false:true; + } + return false; + }); +}; + +/* + * Export constant file + */ +export const file = { + extensionFile:extensionFile, + maxSizeFile:maxSizeFile +}; \ No newline at end of file diff --git a/src/lib/file.js b/src/lib/file.js new file mode 100644 index 0000000..066868f --- /dev/null +++ b/src/lib/file.js @@ -0,0 +1 @@ +export const _isFile = value => (Object.prototype.toString.call(value) === '[object File]') || (Object.prototype.toString.call(value) === '[object Object]') && (value.size!=null && value.name!=null); \ No newline at end of file diff --git a/src/validate.js b/src/validate.js index 7113f94..5d60418 100644 --- a/src/validate.js +++ b/src/validate.js @@ -9,15 +9,18 @@ export * from './number'; export * from './date'; export * from './creditCard'; export * from './ip'; +export * from './file'; export const validate = (...fn) => value => { + // console.log(value); if (isNil(value)) { return false; } return reduce((acc, x) => { + // console.log(x); if (isNil(x)) { throw new Error('The function for validation is null or undefined.'); } else { diff --git a/test/file.test.js b/test/file.test.js new file mode 100644 index 0000000..1cf6380 --- /dev/null +++ b/test/file.test.js @@ -0,0 +1,51 @@ +import expect from 'expect.js'; +import {validate, file} from '../src/validate'; +const fs = require('fs'); + +describe('Functions file', () => { + describe('#extensionFile(value)', () => { + it('should be true when extension of file is equal jpg/png/pdf', () => { + let result = validate(file.extensionFile(["jpg","png","pdf"])); + + expect(result('teste.jpg')).to.be.ok(); + expect(result('teste.png')).to.be.ok(); + expect(result('teste.pdf')).to.be.ok(); + }); + it('should be false when the file extension is different from jpg', () => { + let result = validate(file.extensionFile(["jpg"])); + + expect(result('teste.png')).to.not.be.ok(); + }); + it('should be true when extension of file is equal jpg/png/pdf', () => { + let fileAbstract = {name:"name.jpg",size:250000}; + let result = validate(file.extensionFile(["jpg","png","pdf"])); + + expect(result(fileAbstract)).to.be.ok(); + fileAbstract.name="name.png"; + expect(result(fileAbstract)).to.be.ok(); + fileAbstract.name="name.pdf"; + expect(result(fileAbstract)).to.be.ok(); + }); + it('should be false when the file extension is different from jpg', () => { + let fileAbstract = {name:"name.png",size:250000}; + let result = validate(file.extensionFile(["jpg"])); + + expect(result(fileAbstract)).to.not.be.ok(); + }); + }); + + describe('#maxSizeFile(value)', () => { + it('should be true when size of file in byte is <= maxSize in kilobyte ', () => { + let fileAbstract = {name:"name.jpg",size:1024};//size in Byte + let result = validate(file.maxSizeFile(1));//size in Kilobyte + + expect(result(fileAbstract)).to.be.ok(); + }); + it('should be false when size of file in byte is > maxSize in kilobyte ', () => { + let fileAbstract = {name:"name.jpg",size:1025};//size in Byte + let result = validate(file.maxSizeFile(1));//size in Kilobyte + + expect(result(fileAbstract)).to.not.be.ok(); + }); + }); +}); \ No newline at end of file From 4ffc7034bc8b0ec600221cdae9f4878d4fa6f457 Mon Sep 17 00:00:00 2001 From: paulohrodriguesaifal Date: Mon, 19 Mar 2018 08:24:41 -0300 Subject: [PATCH 2/4] removed console and problem with eslint resolved --- src/validate.js | 4 ++-- test/file.test.js | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/validate.js b/src/validate.js index 5d60418..a6ede21 100644 --- a/src/validate.js +++ b/src/validate.js @@ -14,13 +14,13 @@ export * from './file'; export const validate = (...fn) => value => { - // console.log(value); + if (isNil(value)) { return false; } return reduce((acc, x) => { - // console.log(x); + if (isNil(x)) { throw new Error('The function for validation is null or undefined.'); } else { diff --git a/test/file.test.js b/test/file.test.js index 1cf6380..c475e97 100644 --- a/test/file.test.js +++ b/test/file.test.js @@ -1,34 +1,33 @@ import expect from 'expect.js'; import {validate, file} from '../src/validate'; -const fs = require('fs'); describe('Functions file', () => { describe('#extensionFile(value)', () => { it('should be true when extension of file is equal jpg/png/pdf', () => { - let result = validate(file.extensionFile(["jpg","png","pdf"])); + let result = validate(file.extensionFile(['jpg','png','pdf'])); expect(result('teste.jpg')).to.be.ok(); expect(result('teste.png')).to.be.ok(); expect(result('teste.pdf')).to.be.ok(); }); it('should be false when the file extension is different from jpg', () => { - let result = validate(file.extensionFile(["jpg"])); + let result = validate(file.extensionFile(['jpg'])); expect(result('teste.png')).to.not.be.ok(); }); it('should be true when extension of file is equal jpg/png/pdf', () => { - let fileAbstract = {name:"name.jpg",size:250000}; - let result = validate(file.extensionFile(["jpg","png","pdf"])); + let fileAbstract = {name:'name.jpg',size:250000}; + let result = validate(file.extensionFile(['jpg','png','pdf'])); expect(result(fileAbstract)).to.be.ok(); - fileAbstract.name="name.png"; + fileAbstract.name='name.png'; expect(result(fileAbstract)).to.be.ok(); - fileAbstract.name="name.pdf"; + fileAbstract.name='name.pdf'; expect(result(fileAbstract)).to.be.ok(); }); it('should be false when the file extension is different from jpg', () => { - let fileAbstract = {name:"name.png",size:250000}; - let result = validate(file.extensionFile(["jpg"])); + let fileAbstract = {name:'name.png',size:250000}; + let result = validate(file.extensionFile(['jpg'])); expect(result(fileAbstract)).to.not.be.ok(); }); @@ -36,13 +35,13 @@ describe('Functions file', () => { describe('#maxSizeFile(value)', () => { it('should be true when size of file in byte is <= maxSize in kilobyte ', () => { - let fileAbstract = {name:"name.jpg",size:1024};//size in Byte + let fileAbstract = {name:'name.jpg',size:1024};//size in Byte let result = validate(file.maxSizeFile(1));//size in Kilobyte expect(result(fileAbstract)).to.be.ok(); }); it('should be false when size of file in byte is > maxSize in kilobyte ', () => { - let fileAbstract = {name:"name.jpg",size:1025};//size in Byte + let fileAbstract = {name:'name.jpg',size:1025};//size in Byte let result = validate(file.maxSizeFile(1));//size in Kilobyte expect(result(fileAbstract)).to.not.be.ok(); From c231294864451ba2fc0b726ac636eae56d1e4c99 Mon Sep 17 00:00:00 2001 From: paulohrodriguesaifal Date: Mon, 19 Mar 2018 09:20:25 -0300 Subject: [PATCH 3/4] refactoring code file.js --- src/file.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/file.js b/src/file.js index 8f3e5c1..d7d81a4 100644 --- a/src/file.js +++ b/src/file.js @@ -14,9 +14,8 @@ const extensionFile = exts => { value = _isString(value) ? value: ( _isFile(value) ? value.name : false ); - if(value===false) return false; - let ext = value.substring(value.lastIndexOf('.') + 1); - return ((exts.indexOf(ext)>=0) ? true: false); + let ext = (value===false) ? false: value.substring(value.lastIndexOf('.') + 1); + return (ext===false ? false :exts.indexOf(ext)>=0); }); }; @@ -29,13 +28,9 @@ const extensionFile = exts => { const maxSizeFile = (maxSizeFileValue = Number.MAX_VALUE) => { return (file=>{ - if(!_isNumber(maxSizeFileValue)){ - return false; - } - if(_isFile(file)){ - return ((file.size/1024) > maxSizeFileValue) ? false:true; - } - return false; + if(_isNumber(maxSizeFileValue) && _isFile(file)) + return ( (file.size/1024) <= maxSizeFileValue ); + return false; }); }; From d9ed1d1668ead8047040ad6d45e349bc39a48d78 Mon Sep 17 00:00:00 2001 From: paulohrodriguesaifal Date: Mon, 19 Mar 2018 12:01:51 -0300 Subject: [PATCH 4/4] update dist/validate.js --- dist/validate.js | 2 +- src/file.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dist/validate.js b/dist/validate.js index 99f3a51..dfb5665 100644 --- a/dist/validate.js +++ b/dist/validate.js @@ -1 +1 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,(t.valid||(t.valid={})).js=e()}}(function(){return function e(t,r,n){function i(o,a){if(!r[o]){if(!t[o]){var f="function"==typeof require&&require;if(!a&&f)return f(o,!0);if(u)return u(o,!0);var d=new Error("Cannot find module '"+o+"'");throw d.code="MODULE_NOT_FOUND",d}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){var r=t[o][1][e];return i(r?r:e)},c,c.exports,e,t,r,n)}return r[o].exports}for(var u="function"==typeof require&&require,o=0;o0&&void 0!==arguments[0]?arguments[0]:new Date(3e3,1,1);return function(t){return(0,n._isDate)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1);return function(t){return(0,n._isDate)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1),t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date(3e3,1,1);return function(r){return o(e)(r)&&u(t)(r)}};r.date={maxDate:u,minDate:o,between:a,isDate:i}},{"./lib/date":7}],4:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.file=void 0;var n=e("./lib/string"),i=e("./lib/file"),u=e("./lib/number"),o=function(e){return function(t){if(t=(0,n._isString)(t)?t:!!(0,i._isFile)(t)&&t.name,t===!1)return!1;var r=t.substring(t.lastIndexOf(".")+1);return e.indexOf(r)>=0}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){return!!(0,u._isNumber)(e)&&(!!(0,i._isFile)(t)&&!(t.size/1024>e))}};r.file={extensionFile:o,maxSizeFile:a}},{"./lib/file":8,"./lib/number":10,"./lib/string":13}],5:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=function(e){var t=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return t.test(e)},i=function(e){var t=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;return t.test(e)},u=function(e){return n(e)||i(e)};r.ip={isIp:u,isIpv4:n,isIpv6:i}},{}],6:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isArray=function(e){return null!==e&&e.length>=0&&"[object Array]"===Object.prototype.toString.call(e)}},{}],7:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)}},{}],8:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isFile=function(e){return"[object File]"===Object.prototype.toString.call(e)||"[object Object]"===Object.prototype.toString.call(e)&&null!=e.size&&null!=e.name}},{}],9:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.isNil=function(e){return null===e||void 0===e}},{}],10:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isNumber=function(e){return"[object Number]"===Object.prototype.toString.call(e)}},{}],11:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)}},{}],12:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.reduce=function(e,t,r){if(Array.prototype.reduce)return Array.prototype.reduce.apply(r,[e,t]);var n=r[Symbol.iterator](),i=function u(t,r){var n=r.next(),i=n.value,o=n.done;return o?t:u(e(t,i),r)};return i(t,n)}},{}],13:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isString=function(e){return"[object String]"===Object.prototype.toString.call(e)}},{}],14:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.number=void 0;var n=e("./lib/number"),i=function(e){return(0,n._isNumber)(e)},u=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){return(0,n._isNumber)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,n._isNumber)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&u(t)(r)}};r.number={maxNumber:u,minNumber:o,between:a,isNumber:i}},{"./lib/number":10}],15:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.string=void 0;var n=e("./lib/isnil.js"),i=e("./lib/string"),u=function(e){return(0,i._isString)(e)},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,i._isString)(t)&&t.length>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return(0,n.isNil)(e)&&(e=Number.MAX_VALUE),function(t){return(0,i._isString)(t)&&t.length<=e}},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&a(t)(r)}},d=function(e){return function(t){var r=new RegExp(e);return r.test(t)}};r.string={isString:u,minLength:o,maxLength:a,length:f,regex:d}},{"./lib/isnil.js":9,"./lib/string":13}],16:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.util=void 0;var n=e("./lib/array.js"),i=e("./lib/number.js"),u=e("./lib/object.js"),o=e("./lib/string.js"),a=e("./string.js"),f=function(e){return(0,n._isArray)(e)?e.length>0:(0,i._isNumber)(e)?e>0:(0,u._isObject)(e)?Object.keys(e).length>0:(0,o._isString)(e)?e.length>0:Boolean(e)},d=function(e){var t="[^\\x0d\\x22\\x5c]",r="[^\\x0d\\x5b-\\x5d]",n="[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d]+",i="\\x5c[\\x00-\\x7f]",u="\\x5b("+r+"|"+i+")*\\x5d",o="\\x22("+t+"|"+i+")*\\x22",f=n,d="("+f+"|"+u+")",c="("+n+"|"+o+")",s=d+"(\\x2e"+d+")*",l=c+"(\\x2e"+c+")*",b=l+"\\x40"+s,v="^"+b+"$";return a.string.regex(v)(e)},c=function(e){var t=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;return a.string.regex(t)(e)},s=function(e){return a.string.regex(/^[0-9]{5}-[0-9]{3}$/)(e)},l=function(e){return a.string.regex(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/)(e)},b=function(e){var t=/^(\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-/g,"");if("00000000000"===r||"11111111111"===r||"22222222222"===r||"33333333333"===r||"44444444444"===r||"55555555555"===r||"66666666666"===r||"77777777777"===r||"88888888888"===r||"99999999999"===r)return!1;for(var n=void 0,i=0,u=0;u<9;u++)i+=parseInt(r.charAt(u),10)*(10-u);if(n=11-i%11,10!==n&&11!==n||(n=0),n!==parseInt(r.charAt(9),10))return!1;i=0;for(var o=0;o<10;o++)i+=parseInt(r.charAt(o),10)*(11-o);return n=11-i%11,10!==n&&11!==n||(n=0),n===parseInt(r.charAt(10),10)}return!1},v=function(e){var t=/^(\d{2}[\.]?\d{3}[\.]?\d{3}[\/]?\d{4}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-|\//g,"");if("00000000000000"===r||"11111111111111"===r||"22222222222222"===r||"33333333333333"===r||"44444444444444"===r||"55555555555555"===r||"66666666666666"===r||"77777777777777"===r||"88888888888888"===r||"99999999999999"===r)return!1;for(var n=0,i=12,u=r.substring(0,i),o=r.substring(i),a=i-7,f=i;f>=1;f--)n+=u.charAt(i-f)*a--,a<2&&(a=9);var d=n%11<2?0:11-n%11;if(d!==parseInt(o.charAt(0),10))return!1;i+=1,u=r.substring(0,i),n=0,a=i-7;for(var c=i;c>=1;c--)n+=u.charAt(i-c)*a--,a<2&&(a=9);return d=n%11<2?0:11-n%11,d===parseInt(o.charAt(1),10)}return!1};r.util={isRequired:f,isEmail:d,isCep:s,isURL:c,isHexColor:l,isCpf:b,isCnpj:v}},{"./lib/array.js":6,"./lib/number.js":10,"./lib/object.js":11,"./lib/string.js":13,"./string.js":15}],17:[function(e,t,r){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t1&&void 0!==arguments[1]?arguments[1]:[];return _.apply(void 0,n((0,b._isArray)(t)?t:[t]))(e)}},{"./boolean":1,"./creditCard":2,"./date":3,"./file":4,"./ip":5,"./lib/array.js":6,"./lib/isnil.js":9,"./lib/reduce.js":12,"./number":14,"./string":15,"./util":16}]},{},[17])(17)}); \ No newline at end of file +!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,(t.valid||(t.valid={})).js=e()}}(function(){return function e(t,r,n){function i(o,a){if(!r[o]){if(!t[o]){var f="function"==typeof require&&require;if(!a&&f)return f(o,!0);if(u)return u(o,!0);var d=new Error("Cannot find module '"+o+"'");throw d.code="MODULE_NOT_FOUND",d}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){var r=t[o][1][e];return i(r?r:e)},c,c.exports,e,t,r,n)}return r[o].exports}for(var u="function"==typeof require&&require,o=0;o0&&void 0!==arguments[0]?arguments[0]:new Date(3e3,1,1);return function(t){return(0,n._isDate)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1);return function(t){return(0,n._isDate)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date(1900,1,1),t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date(3e3,1,1);return function(r){return o(e)(r)&&u(t)(r)}};r.date={maxDate:u,minDate:o,between:a,isDate:i}},{"./lib/date":7}],4:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.file=void 0;var n=e("./lib/string"),i=e("./lib/file"),u=e("./lib/number"),o=function(e){return function(t){t=(0,n._isString)(t)?t:!!(0,i._isFile)(t)&&t.name;var r=t!==!1&&t.substring(t.lastIndexOf(".")+1);return r!==!1&&e.indexOf(r)>=0}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){if((0,u._isNumber)(e)&&(0,i._isFile)(t))return t.size/1024<=e}};r.file={extensionFile:o,maxSizeFile:a}},{"./lib/file":8,"./lib/number":10,"./lib/string":13}],5:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=function(e){var t=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return t.test(e)},i=function(e){var t=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;return t.test(e)},u=function(e){return n(e)||i(e)};r.ip={isIp:u,isIpv4:n,isIpv6:i}},{}],6:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isArray=function(e){return null!==e&&e.length>=0&&"[object Array]"===Object.prototype.toString.call(e)}},{}],7:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)}},{}],8:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isFile=function(e){return"[object File]"===Object.prototype.toString.call(e)||"[object Object]"===Object.prototype.toString.call(e)&&null!=e.size&&null!=e.name}},{}],9:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.isNil=function(e){return null===e||void 0===e}},{}],10:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isNumber=function(e){return"[object Number]"===Object.prototype.toString.call(e)}},{}],11:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)}},{}],12:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r.reduce=function(e,t,r){if(Array.prototype.reduce)return Array.prototype.reduce.apply(r,[e,t]);var n=r[Symbol.iterator](),i=function u(t,r){var n=r.next(),i=n.value,o=n.done;return o?t:u(e(t,i),r)};return i(t,n)}},{}],13:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});r._isString=function(e){return"[object String]"===Object.prototype.toString.call(e)}},{}],14:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.number=void 0;var n=e("./lib/number"),i=function(e){return(0,n._isNumber)(e)},u=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return function(t){return(0,n._isNumber)(t)&&t<=e}},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,n._isNumber)(t)&&t>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&u(t)(r)}};r.number={maxNumber:u,minNumber:o,between:a,isNumber:i}},{"./lib/number":10}],15:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.string=void 0;var n=e("./lib/isnil.js"),i=e("./lib/string"),u=function(e){return(0,i._isString)(e)},o=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE;return function(t){return(0,i._isString)(t)&&t.length>=e}},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_VALUE;return(0,n.isNil)(e)&&(e=Number.MAX_VALUE),function(t){return(0,i._isString)(t)&&t.length<=e}},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MIN_VALUE,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Number.MAX_VALUE;return function(r){return o(e)(r)&&a(t)(r)}},d=function(e){return function(t){var r=new RegExp(e);return r.test(t)}};r.string={isString:u,minLength:o,maxLength:a,length:f,regex:d}},{"./lib/isnil.js":9,"./lib/string":13}],16:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.util=void 0;var n=e("./lib/array.js"),i=e("./lib/number.js"),u=e("./lib/object.js"),o=e("./lib/string.js"),a=e("./string.js"),f=function(e){return(0,n._isArray)(e)?e.length>0:(0,i._isNumber)(e)?e>0:(0,u._isObject)(e)?Object.keys(e).length>0:(0,o._isString)(e)?e.length>0:Boolean(e)},d=function(e){var t="[^\\x0d\\x22\\x5c]",r="[^\\x0d\\x5b-\\x5d]",n="[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d]+",i="\\x5c[\\x00-\\x7f]",u="\\x5b("+r+"|"+i+")*\\x5d",o="\\x22("+t+"|"+i+")*\\x22",f=n,d="("+f+"|"+u+")",c="("+n+"|"+o+")",s=d+"(\\x2e"+d+")*",l=c+"(\\x2e"+c+")*",b=l+"\\x40"+s,v="^"+b+"$";return a.string.regex(v)(e)},c=function(e){var t=/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i;return a.string.regex(t)(e)},s=function(e){return a.string.regex(/^[0-9]{5}-[0-9]{3}$/)(e)},l=function(e){return a.string.regex(/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/)(e)},b=function(e){var t=/^(\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-/g,"");if("00000000000"===r||"11111111111"===r||"22222222222"===r||"33333333333"===r||"44444444444"===r||"55555555555"===r||"66666666666"===r||"77777777777"===r||"88888888888"===r||"99999999999"===r)return!1;for(var n=void 0,i=0,u=0;u<9;u++)i+=parseInt(r.charAt(u),10)*(10-u);if(n=11-i%11,10!==n&&11!==n||(n=0),n!==parseInt(r.charAt(9),10))return!1;i=0;for(var o=0;o<10;o++)i+=parseInt(r.charAt(o),10)*(11-o);return n=11-i%11,10!==n&&11!==n||(n=0),n===parseInt(r.charAt(10),10)}return!1},v=function(e){var t=/^(\d{2}[\.]?\d{3}[\.]?\d{3}[\/]?\d{4}[-]?\d{2})$/;if(t.test(e)){var r=e.replace(/\.|-|\//g,"");if("00000000000000"===r||"11111111111111"===r||"22222222222222"===r||"33333333333333"===r||"44444444444444"===r||"55555555555555"===r||"66666666666666"===r||"77777777777777"===r||"88888888888888"===r||"99999999999999"===r)return!1;for(var n=0,i=12,u=r.substring(0,i),o=r.substring(i),a=i-7,f=i;f>=1;f--)n+=u.charAt(i-f)*a--,a<2&&(a=9);var d=n%11<2?0:11-n%11;if(d!==parseInt(o.charAt(0),10))return!1;i+=1,u=r.substring(0,i),n=0,a=i-7;for(var c=i;c>=1;c--)n+=u.charAt(i-c)*a--,a<2&&(a=9);return d=n%11<2?0:11-n%11,d===parseInt(o.charAt(1),10)}return!1};r.util={isRequired:f,isEmail:d,isCep:s,isURL:c,isHexColor:l,isCpf:b,isCnpj:v}},{"./lib/array.js":6,"./lib/number.js":10,"./lib/object.js":11,"./lib/string.js":13,"./string.js":15}],17:[function(e,t,r){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t1&&void 0!==arguments[1]?arguments[1]:[];return _.apply(void 0,n((0,b._isArray)(t)?t:[t]))(e)}},{"./boolean":1,"./creditCard":2,"./date":3,"./file":4,"./ip":5,"./lib/array.js":6,"./lib/isnil.js":9,"./lib/reduce.js":12,"./number":14,"./string":15,"./util":16}]},{},[17])(17)}); \ No newline at end of file diff --git a/src/file.js b/src/file.js index d7d81a4..9177d6d 100644 --- a/src/file.js +++ b/src/file.js @@ -29,8 +29,7 @@ const extensionFile = exts => { const maxSizeFile = (maxSizeFileValue = Number.MAX_VALUE) => { return (file=>{ if(_isNumber(maxSizeFileValue) && _isFile(file)) - return ( (file.size/1024) <= maxSizeFileValue ); - return false; + return ( (file.size/1024) <= maxSizeFileValue ); }); };