From 33ac6ad7cce93033d285bf71391540f7e52fe9c5 Mon Sep 17 00:00:00 2001 From: Charles Kenney Date: Mon, 17 Jul 2017 17:00:12 -0400 Subject: [PATCH 1/5] added latlong validator --- index.js | 5 ++++ lib/isLatLong.js | 23 +++++++++++++++++ src/index.js | 3 +++ src/lib/isLatLong.js | 11 +++++++++ test/validators.js | 59 ++++++++++++++++++++++++++++++++++++++++++++ validator.js | 11 +++++++++ validator.min.js | 2 +- 7 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 lib/isLatLong.js create mode 100644 src/lib/isLatLong.js diff --git a/index.js b/index.js index 8f5b21ee0..bff650b1c 100644 --- a/index.js +++ b/index.js @@ -204,6 +204,10 @@ var _isDataURI = require('./lib/isDataURI'); var _isDataURI2 = _interopRequireDefault(_isDataURI); +var _isLatLong = require('./lib/isLatLong'); + +var _isLatLong2 = _interopRequireDefault(_isLatLong); + var _ltrim = require('./lib/ltrim'); var _ltrim2 = _interopRequireDefault(_ltrim); @@ -304,6 +308,7 @@ var validator = { isISO8601: _isISO2.default, isBase64: _isBase2.default, isDataURI: _isDataURI2.default, + isLatLong: _isLatLong2.default, ltrim: _ltrim2.default, rtrim: _rtrim2.default, trim: _trim2.default, diff --git a/lib/isLatLong.js b/lib/isLatLong.js new file mode 100644 index 000000000..230d9ad58 --- /dev/null +++ b/lib/isLatLong.js @@ -0,0 +1,23 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +exports.default = function (str) { + (0, _assertString2.default)(str); + if (!str.includes(',')) return false; + var pair = str.split(','); + return lat.test(pair[0]) && long.test(pair[1]); +}; + +var _assertString = require('./util/assertString'); + +var _assertString2 = _interopRequireDefault(_assertString); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; +var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; + +module.exports = exports['default']; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 752720a91..9372f6c84 100644 --- a/src/index.js +++ b/src/index.js @@ -68,6 +68,8 @@ import isISO8601 from './lib/isISO8601'; import isBase64 from './lib/isBase64'; import isDataURI from './lib/isDataURI'; +import isLatLong from './lib/isLatLong'; + import ltrim from './lib/ltrim'; import rtrim from './lib/rtrim'; import trim from './lib/trim'; @@ -136,6 +138,7 @@ const validator = { isISO8601, isBase64, isDataURI, + isLatLong, ltrim, rtrim, trim, diff --git a/src/lib/isLatLong.js b/src/lib/isLatLong.js new file mode 100644 index 000000000..ec401575e --- /dev/null +++ b/src/lib/isLatLong.js @@ -0,0 +1,11 @@ +import assertString from './util/assertString'; + +const lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; +const long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; + +export default function (str) { + assertString(str); + if (!str.includes(',')) return false; + const pair = str.split(','); + return lat.test(pair[0]) && long.test(pair[1]); +} diff --git a/test/validators.js b/test/validators.js index 315099357..e72972507 100644 --- a/test/validators.js +++ b/test/validators.js @@ -3796,4 +3796,63 @@ describe('Validators', function () { }); /* eslint-enable max-len */ }); + + it('should validate LatLong', function () { + test({ + validator: 'isLatLong', + valid: [ + '(-17.738223, 85.605469)', + '(-12.3456789, +12.3456789)', + '(-60.978437, -0.175781)', + '(77.719772, -37.529297)', + '(7.264394, 165.058594)', + '0.955766, -19.863281', + '(31.269161,164.355469)', + '+12.3456789, -12.3456789', + '-15.379543, -137.285156', + '(11.770570, -162.949219)', + '-55.034319, 113.027344', + '58.025555, 36.738281', + '55.720923,-28.652344', + '-90.00000,-180.00000', + '(-71, -146)', + '(-71.616864, -146.616864)', + '-0.55, +0.22', + '90, 180', + '+90, -180', + '-90,+180', + '90,180', + '0, 0', + ], + invalid: [ + '(020.000000, 010.000000000)', + '89.9999999989, 360.0000000', + '90.1000000, 180.000000', + '+90.000000, -180.00001', + '090.0000, 0180.0000', + '126, -158', + '(-126.400010, -158.400010)', + '-95, -96', + '-95.738043, -96.738043', + '137, -148', + '(-137.5942, -148.5942)', + '(-120, -203)', + '(-119, -196)', + '+119.821728, -196.821728', + '(-110, -223)', + '-110.369532, 223.369532', + '(-120.969949, +203.969949)',, + '-116, -126', + '-116.894222, -126.894222', + '-112, -160', + '-112.96381, -160.96381', + '-90., -180.', + '+90.1, -180.1', + '+,-', + '(,)', + ',', + ' ', + ], + }); + }); }); diff --git a/validator.js b/validator.js index c5967c356..8e568ab34 100644 --- a/validator.js +++ b/validator.js @@ -1055,6 +1055,16 @@ function isDataURI(str) { return dataURI.test(str); } +var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; +var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; + +var isLatLong = function (str) { + assertString(str); + if (!str.includes(',')) return false; + var pair = str.split(','); + return lat.test(pair[0]) && long.test(pair[1]); +}; + function ltrim(str, chars) { assertString(str); var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g; @@ -1287,6 +1297,7 @@ var validator = { isISO8601: isISO8601, isBase64: isBase64, isDataURI: isDataURI, + isLatLong: isLatLong, ltrim: ltrim, rtrim: rtrim, trim: trim, diff --git a/validator.min.js b/validator.min.js index e965a22f6..7e25cb347 100644 --- a/validator.min.js +++ b/validator.min.js @@ -20,4 +20,4 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,F)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!k.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!et.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!rt.test(o))return!1;for(n=0;n<12;n++)i+=ot[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function p(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function g(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,A=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,w=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,y=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,b=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,k=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,D=/^[0-9A-F]{1,4}$/i,S={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},E=/^\[([^\]]+)\](?::([0-9]+))?$/,Z=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,O={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},R={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},C=["AU","GB","HK","IN","NZ","ZA","ZM"],I=0;I=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,S);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,p=void 0,g=void 0;if(p=e.split("#"),e=p.shift(),p=e.split("?"),e=p.shift(),(p=e.split("://")).length>1){if(o=p.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(p[0]=e.substr(2))}if(""===(e=p.join("://")))return!1;if(p=e.split("/"),""===(e=p.shift())&&!r.require_host)return!0;if((p=e.split("@")).length>1&&(n=p.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,g=null;var h=(s=p.join("@")).match(E);return h?(a="",g=h[1],f=h[2]||null):(a=(p=s.split(":")).shift(),p.length&&(f=p.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||g&&u(g,6))||(a=a||g,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),Z.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in O)return O[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in R)return R[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),B.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),P.test(e)},isFullWidth:function(e){return t(e),L.test(e)},isHalfWidth:function(e){return t(e),N.test(e)},isVariableWidth:function(e){return t(e),L.test(e)&&N.test(e)},isMultibyte:function(e){return t(e),q.test(e)},isSurrogatePair:function(e){return t(e),T.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?H:K,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&M.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&W.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),Y.test(e)},isISRC:function(e){return t(e),J.test(e)},isMD5:function(e){return t(e),Q.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=X[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!V.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!tt.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=it;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in nt)return nt[r].test(e);if("any"===r)return!!Object.values(nt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,lt),p(r).test(e)},isISO8601:function(e){return t(e),at.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||ut.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),st.test(e)},ltrim:g,rtrim:h,trim:function(t,e){return h(g(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,dt),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~ct.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~ft.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~pt.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,F)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!k.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!et.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!rt.test(o))return!1;for(n=0;n<12;n++)i+=ot[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function p(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function g(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,A=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,w=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,y=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,b=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,k=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,D=/^[0-9A-F]{1,4}$/i,S={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},E=/^\[([^\]]+)\](?::([0-9]+))?$/,Z=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,O={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},R={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},C=["AU","GB","HK","IN","NZ","ZA","ZM"],I=0;I=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,S);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,p=void 0,g=void 0;if(p=e.split("#"),e=p.shift(),p=e.split("?"),e=p.shift(),(p=e.split("://")).length>1){if(o=p.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(p[0]=e.substr(2))}if(""===(e=p.join("://")))return!1;if(p=e.split("/"),""===(e=p.shift())&&!r.require_host)return!0;if((p=e.split("@")).length>1&&(n=p.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,g=null;var h=(s=p.join("@")).match(E);return h?(a="",g=h[1],f=h[2]||null):(a=(p=s.split(":")).shift(),p.length&&(f=p.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||g&&u(g,6))||(a=a||g,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),Z.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in O)return O[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in R)return R[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),L.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),B.test(e)},isFullWidth:function(e){return t(e),P.test(e)},isHalfWidth:function(e){return t(e),N.test(e)},isVariableWidth:function(e){return t(e),P.test(e)&&N.test(e)},isMultibyte:function(e){return t(e),q.test(e)},isSurrogatePair:function(e){return t(e),T.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?H:K,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&M.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&W.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),Y.test(e)},isISRC:function(e){return t(e),J.test(e)},isMD5:function(e){return t(e),Q.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=X[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!V.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!tt.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=it;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in nt)return nt[r].test(e);if("any"===r)return!!Object.values(nt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,lt),p(r).test(e)},isISO8601:function(e){return t(e),at.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||ut.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),st.test(e)},isLatLong:function(e){if(t(e),!e.includes(","))return!1;var r=e.split(",");return dt.test(r[0])&&ct.test(r[1])},ltrim:g,rtrim:h,trim:function(t,e){return h(g(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,ft),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~pt.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~gt.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~ht.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file From ea1cc003f258c9df45b91f22fdaf3e17850aed7f Mon Sep 17 00:00:00 2001 From: Charles Kenney Date: Tue, 18 Jul 2017 01:29:38 -0400 Subject: [PATCH 2/5] =?UTF-8?q?added=20postal=20code=20validator=20?= =?UTF-8?q?=F0=9F=93=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 5 ++ lib/isPostalCode.js | 71 ++++++++++++++++ src/index.js | 2 + src/lib/isPostalCode.js | 55 ++++++++++++ test/validators.js | 180 +++++++++++++++++++++++++++++++++++++++- validator.js | 121 +++++++++++++++++++++++++++ validator.min.js | 2 +- 7 files changed, 434 insertions(+), 2 deletions(-) create mode 100644 lib/isPostalCode.js create mode 100644 src/lib/isPostalCode.js diff --git a/index.js b/index.js index bff650b1c..1d5b76eef 100644 --- a/index.js +++ b/index.js @@ -208,6 +208,10 @@ var _isLatLong = require('./lib/isLatLong'); var _isLatLong2 = _interopRequireDefault(_isLatLong); +var _isPostalCode = require('./lib/isPostalCode'); + +var _isPostalCode2 = _interopRequireDefault(_isPostalCode); + var _ltrim = require('./lib/ltrim'); var _ltrim2 = _interopRequireDefault(_ltrim); @@ -304,6 +308,7 @@ var validator = { isISBN: _isISBN2.default, isISSN: _isISSN2.default, isMobilePhone: _isMobilePhone2.default, + isPostalCode: _isPostalCode2.default, isCurrency: _isCurrency2.default, isISO8601: _isISO2.default, isBase64: _isBase2.default, diff --git a/lib/isPostalCode.js b/lib/isPostalCode.js new file mode 100644 index 000000000..ab04b2b44 --- /dev/null +++ b/lib/isPostalCode.js @@ -0,0 +1,71 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +exports.default = function (str, locale) { + (0, _assertString2.default)(str); + if (locale in patterns) { + return patterns[locale].test(str); + } else if (locale === 'any') { + // Test each unique pattern + return !![].concat(_toConsumableArray(new Set(Object.values(patterns)))).find(function (pattern) { + return pattern.test(str); + }); + } + throw new Error('Invalid locale \'' + locale + '\''); +}; + +var _assertString = require('./util/assertString'); + +var _assertString2 = _interopRequireDefault(_assertString); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + +// common patterns +var threeDigit = /^\d{3}$/; +var fourDigit = /^\d{4}$/; +var fiveDigit = /^\d{5}$/; +var sixDigit = /^\d{6}$/; + +var patterns = { + AT: fourDigit, + AU: sixDigit, + BE: fourDigit, + CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i, + CH: fourDigit, + CZ: /^\d{3}\s?\d{2}$/, + DE: fiveDigit, + DK: fourDigit, + DZ: fiveDigit, + ES: fiveDigit, + FI: fiveDigit, + FR: /^\d{2}\s?\d{3}$/, + GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i, + GR: /^\d{3}\s?\d{2}$/, + IL: fiveDigit, + IN: sixDigit, + IS: threeDigit, + IT: fiveDigit, + JP: /^\d{3}\-\d{4}$/, + KE: fiveDigit, + LI: /^(948[5-9]|949[0-7])$/, + MX: fiveDigit, + NL: /^\d{4}\s?[a-z]{2}$/i, + NO: fourDigit, + PL: /^\d{2}\-\d{3}$/, + PT: /^\d{4}(\-\d{3})?$/, + RO: sixDigit, + RU: sixDigit, + SA: fiveDigit, + SE: /^\d{3}\s?\d{2}$/, + TW: /^\d{3}(\d{2})?$/, + US: /^\d{5}(-\d{4})?$/, + ZA: fourDigit, + ZM: fiveDigit +}; + +module.exports = exports['default']; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 9372f6c84..adcf4d694 100644 --- a/src/index.js +++ b/src/index.js @@ -69,6 +69,7 @@ import isBase64 from './lib/isBase64'; import isDataURI from './lib/isDataURI'; import isLatLong from './lib/isLatLong'; +import isPostalCode from './lib/isPostalCode'; import ltrim from './lib/ltrim'; import rtrim from './lib/rtrim'; @@ -134,6 +135,7 @@ const validator = { isISBN, isISSN, isMobilePhone, + isPostalCode, isCurrency, isISO8601, isBase64, diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js new file mode 100644 index 000000000..a38c8bab0 --- /dev/null +++ b/src/lib/isPostalCode.js @@ -0,0 +1,55 @@ +import assertString from './util/assertString'; + +// common patterns +const threeDigit = /^\d{3}$/; +const fourDigit = /^\d{4}$/; +const fiveDigit = /^\d{5}$/; +const sixDigit = /^\d{6}$/; + +const patterns = { + AT: fourDigit, + AU: sixDigit, + BE: fourDigit, + CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i, + CH: fourDigit, + CZ: /^\d{3}\s?\d{2}$/, + DE: fiveDigit, + DK: fourDigit, + DZ: fiveDigit, + ES: fiveDigit, + FI: fiveDigit, + FR: /^\d{2}\s?\d{3}$/, + GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i, + GR: /^\d{3}\s?\d{2}$/, + IL: fiveDigit, + IN: sixDigit, + IS: threeDigit, + IT: fiveDigit, + JP: /^\d{3}\-\d{4}$/, + KE: fiveDigit, + LI: /^(948[5-9]|949[0-7])$/, + MX: fiveDigit, + NL: /^\d{4}\s?[a-z]{2}$/i, + NO: fourDigit, + PL: /^\d{2}\-\d{3}$/, + PT: /^\d{4}(\-\d{3})?$/, + RO: sixDigit, + RU: sixDigit, + SA: fiveDigit, + SE: /^\d{3}\s?\d{2}$/, + TW: /^\d{3}(\d{2})?$/, + US: /^\d{5}(-\d{4})?$/, + ZA: fourDigit, + ZM: fiveDigit, +}; + +export default function (str, locale) { + assertString(str); + if (locale in patterns) { + return patterns[locale].test(str); + } else if (locale === 'any') { + // Test each unique pattern + return !![...new Set(Object.values(patterns))].find(pattern => pattern.test(str)); + } + throw new Error(`Invalid locale '${locale}'`); +} diff --git a/test/validators.js b/test/validators.js index e72972507..4091eaaa2 100644 --- a/test/validators.js +++ b/test/validators.js @@ -3841,7 +3841,7 @@ describe('Validators', function () { '+119.821728, -196.821728', '(-110, -223)', '-110.369532, 223.369532', - '(-120.969949, +203.969949)',, + '(-120.969949, +203.969949)', '-116, -126', '-116.894222, -126.894222', '-112, -160', @@ -3855,4 +3855,182 @@ describe('Validators', function () { ], }); }); + + it('should validate postal code', function () { + const fixtures = [ + { + locale: 'CA', + valid: [ + 'L4T 0A5', + 'G1A-0A2', + 'A1A 1A1', + 'X0A-0H0', + 'V5K 0A1', + ], + }, + { + locale: 'JP', + valid: [ + '135-0000', + '874-8577', + '669-1161', + '470-0156', + '672-8031', + ], + }, + { + locale: 'GR', + valid: [ + '022 93', + '29934', + '90293', + '299 42', + '94944', + ], + }, + { + locale: 'GB', + valid: [ + 'TW8 9GS', + 'BS98 1TL', + 'DE99 3GG', + 'DE55 4SW', + 'DH98 1BT', + 'DH99 1NS', + 'GIR0aa', + 'SA99', + 'W1N 4DJ', + 'AA9A 9AA', + 'AA99 9AA', + 'BS98 1TL', + 'DE993GG', + ], + }, + { + locale: 'FR', + valid: [ + '75008', + '44 522', + '98025', + '38 499', + '39940', + ], + }, + { + locale: 'CZ', + valid: [ + '20134', + '392 90', + '39919', + '938 29', + '39949', + ], + }, + { + locale: 'NL', + valid: [ + '1012 SZ', + '3432FE', + '1118 BH', + '3950IO', + '3997 GH', + ], + }, + { + locale: 'PL', + valid: [ + '47-260', + '12-930', + '78-399', + '39-490', + '38-483', + ], + }, + { + locale: 'TW', + valid: [ + '360', + '90312', + '399', + '935', + '38842', + ], + }, + { + locale: 'LI', + valid: [ + '9485', + '9497', + '9491', + '9489', + '9496', + ], + }, + { + locale: 'PT', + valid: [ + '4827', + '4829-489', + '0294-348', + '1928', + '8156-392', + ], + }, + { + locale: 'SE', + valid: [ + '12994', + '284 39', + '39556', + '489 39', + '499 49', + ], + }, + ]; + + let allValid = []; + + // Test fixtures + fixtures.forEach(function (fixture) { + if (fixture.valid) allValid = allValid.concat(fixture.valid); + test({ + validator: 'isPostalCode', + valid: fixture.valid, + invalid: fixture.invalid, + args: [fixture.locale], + }); + }); + + // Test generics + test({ + validator: 'isPostalCode', + valid: [ + ...allValid, + '1234', + '6900', + '1292', + '9400', + '27616', + '90210', + '10001', + '21201', + '33142', + '060623', + '123456', + '293940', + '002920', + ], + invalid: [ + 'asdf', + '1', + 'ASDFGJKLmZXJtZtesting123', + 'Vml2YW11cyBmZXJtZtesting123', + '48380480343', + '29923-329393-2324', + '4294924224', + '13', + ], + args: ['any'], + }); + }); }); diff --git a/validator.js b/validator.js index 8e568ab34..1e4ef303f 100644 --- a/validator.js +++ b/validator.js @@ -69,6 +69,70 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +var toConsumableArray = function (arr) { + if (Array.isArray(arr)) { + for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; + + return arr2; + } else { + return Array.from(arr); + } +}; + function toString(input) { if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && input !== null) { if (typeof input.toString === 'function') { @@ -1065,6 +1129,62 @@ var isLatLong = function (str) { return lat.test(pair[0]) && long.test(pair[1]); }; +// common patterns +var threeDigit = /^\d{3}$/; +var fourDigit = /^\d{4}$/; +var fiveDigit = /^\d{5}$/; +var sixDigit = /^\d{6}$/; + +var patterns = { + AT: fourDigit, + AU: sixDigit, + BE: fourDigit, + CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i, + CH: fourDigit, + CZ: /^\d{3}\s?\d{2}$/, + DE: fiveDigit, + DK: fourDigit, + DZ: fiveDigit, + ES: fiveDigit, + FI: fiveDigit, + FR: /^\d{2}\s?\d{3}$/, + GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i, + GR: /^\d{3}\s?\d{2}$/, + IL: fiveDigit, + IN: sixDigit, + IS: threeDigit, + IT: fiveDigit, + JP: /^\d{3}\-\d{4}$/, + KE: fiveDigit, + LI: /^(948[5-9]|949[0-7])$/, + MX: fiveDigit, + NL: /^\d{4}\s?[a-z]{2}$/i, + NO: fourDigit, + PL: /^\d{2}\-\d{3}$/, + PT: /^\d{4}(\-\d{3})?$/, + RO: sixDigit, + RU: sixDigit, + SA: fiveDigit, + SE: /^\d{3}\s?\d{2}$/, + TW: /^\d{3}(\d{2})?$/, + US: /^\d{5}(-\d{4})?$/, + ZA: fourDigit, + ZM: fiveDigit +}; + +var isPostalCode = function (str, locale) { + assertString(str); + if (locale in patterns) { + return patterns[locale].test(str); + } else if (locale === 'any') { + // Test each unique pattern + return !![].concat(toConsumableArray(new Set(Object.values(patterns)))).find(function (pattern) { + return pattern.test(str); + }); + } + throw new Error('Invalid locale \'' + locale + '\''); +}; + function ltrim(str, chars) { assertString(str); var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g; @@ -1293,6 +1413,7 @@ var validator = { isISBN: isISBN, isISSN: isISSN, isMobilePhone: isMobilePhone, + isPostalCode: isPostalCode, isCurrency: isCurrency, isISO8601: isISO8601, isBase64: isBase64, diff --git a/validator.min.js b/validator.min.js index 7e25cb347..d14a04053 100644 --- a/validator.min.js +++ b/validator.min.js @@ -20,4 +20,4 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,F)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!k.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!et.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!rt.test(o))return!1;for(n=0;n<12;n++)i+=ot[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function p(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function g(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,A=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,w=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,y=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,b=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,k=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,D=/^[0-9A-F]{1,4}$/i,S={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},E=/^\[([^\]]+)\](?::([0-9]+))?$/,Z=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,O={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},R={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},C=["AU","GB","HK","IN","NZ","ZA","ZM"],I=0;I=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,S);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,p=void 0,g=void 0;if(p=e.split("#"),e=p.shift(),p=e.split("?"),e=p.shift(),(p=e.split("://")).length>1){if(o=p.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(p[0]=e.substr(2))}if(""===(e=p.join("://")))return!1;if(p=e.split("/"),""===(e=p.shift())&&!r.require_host)return!0;if((p=e.split("@")).length>1&&(n=p.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,g=null;var h=(s=p.join("@")).match(E);return h?(a="",g=h[1],f=h[2]||null):(a=(p=s.split(":")).shift(),p.length&&(f=p.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||g&&u(g,6))||(a=a||g,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),Z.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in O)return O[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in R)return R[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),L.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),B.test(e)},isFullWidth:function(e){return t(e),P.test(e)},isHalfWidth:function(e){return t(e),N.test(e)},isVariableWidth:function(e){return t(e),P.test(e)&&N.test(e)},isMultibyte:function(e){return t(e),q.test(e)},isSurrogatePair:function(e){return t(e),T.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?H:K,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&M.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&W.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),Y.test(e)},isISRC:function(e){return t(e),J.test(e)},isMD5:function(e){return t(e),Q.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=X[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!V.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!tt.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=it;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in nt)return nt[r].test(e);if("any"===r)return!!Object.values(nt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,lt),p(r).test(e)},isISO8601:function(e){return t(e),at.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||ut.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),st.test(e)},isLatLong:function(e){if(t(e),!e.includes(","))return!1;var r=e.split(",");return dt.test(r[0])&&ct.test(r[1])},ltrim:g,rtrim:h,trim:function(t,e){return h(g(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,ft),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~pt.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~gt.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~ht.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,F)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!E.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!rt.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!ot.test(o))return!1;for(n=0;n<12;n++)i+=it[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function g(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function p(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,w=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,y=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,b=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,S=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,E=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,k=/^[0-9A-F]{1,4}$/i,D={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},Z=/^\[([^\]]+)\](?::([0-9]+))?$/,R=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,C={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},I={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},O=["AU","GB","HK","IN","NZ","ZA","ZM"],P=0;P=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,D);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,g=void 0,p=void 0;if(g=e.split("#"),e=g.shift(),g=e.split("?"),e=g.shift(),(g=e.split("://")).length>1){if(o=g.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(g[0]=e.substr(2))}if(""===(e=g.join("://")))return!1;if(g=e.split("/"),""===(e=g.shift())&&!r.require_host)return!0;if((g=e.split("@")).length>1&&(n=g.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,p=null;var h=(s=g.join("@")).match(Z);return h?(a="",p=h[1],f=h[2]||null):(a=(g=s.split(":")).shift(),g.length&&(f=g.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||p&&u(p,6))||(a=a||p,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),R.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in C)return C[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in I)return I[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),B.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),N.test(e)},isFullWidth:function(e){return t(e),j.test(e)},isHalfWidth:function(e){return t(e),T.test(e)},isVariableWidth:function(e){return t(e),j.test(e)&&T.test(e)},isMultibyte:function(e){return t(e),q.test(e)},isSurrogatePair:function(e){return t(e),H.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?K:M,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&G.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&W.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),X.test(e)},isISRC:function(e){return t(e),Y.test(e)},isMD5:function(e){return t(e),V.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=Q[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!tt.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!et.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=nt;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in lt)return lt[r].test(e);if("any"===r)return!!Object.values(lt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isPostalCode:function(e,r){if(t(e),r in mt)return mt[r].test(e);if("any"===r)return!![].concat($(new Set(Object.values(mt)))).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,at),g(r).test(e)},isISO8601:function(e){return t(e),ut.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||st.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),dt.test(e)},isLatLong:function(e){if(t(e),!e.includes(","))return!1;var r=e.split(",");return ct.test(r[0])&&ft.test(r[1])},ltrim:p,rtrim:h,trim:function(t,e){return h(p(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,_t),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~vt.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~$t.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~Ft.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file From 9e6727d88a71fdcee72e23bf85563974aab5821f Mon Sep 17 00:00:00 2001 From: Charles Kenney Date: Tue, 18 Jul 2017 01:50:28 -0400 Subject: [PATCH 3/5] update readme with new features --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f9abe9350..0a3d36661 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Passing anything other than a string is an error. - **isIn(str, values)** - check if the string is in a array of allowed values. - **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). - **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse). +- **isLatLong(str)** - check if the string is a valid latitude-longitude coordinate. - **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. - **isLowercase(str)** - check if the string is lowercase. - **isMACAddress(str)** - check if the string is a MAC address. @@ -92,6 +93,7 @@ Passing anything other than a string is an error. - **isMongoId(str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid]. - **isMultibyte(str)** - check if the string contains one or more multibyte chars. - **isNumeric(str)** - check if the string contains only numbers. +- **isPostalCode(str, locale)** - check if the string is a postal code, (locale is one of `[ 'AT','AU','BE','CA','CH','CZ','DE','DK','DZ','ES','FI','FR','GB','GR','IL','IN','IS','IT','JP','KE','LI','MX','NL','NO','PL','PT','RO','RU','SA','SE','TW','US','ZA','ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match). - **isSurrogatePair(str)** - check if the string contains any surrogate pairs chars. - **isURL(str [, options])** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }`. - **isUUID(str [, version])** - check if the string is a UUID (version 3, 4 or 5). From bacfd2306aaeacad761e5a9905f7a93a653d26e4 Mon Sep 17 00:00:00 2001 From: Charles Kenney Date: Fri, 21 Jul 2017 17:58:22 -0400 Subject: [PATCH 4/5] removed whitespace in validator.js --- validator.js | 54 ---------------------------------------------------- 1 file changed, 54 deletions(-) diff --git a/validator.js b/validator.js index 1e4ef303f..fd2796c4a 100644 --- a/validator.js +++ b/validator.js @@ -69,60 +69,6 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - var toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; From 63cf4063d295a414db25c14433276b5c86fe7612 Mon Sep 17 00:00:00 2001 From: Charles Kenney Date: Tue, 22 Aug 2017 05:14:54 -0400 Subject: [PATCH 5/5] Don't use Object.values --- lib/isPostalCode.js | 15 +++++++++------ src/lib/isPostalCode.js | 11 +++++++++-- validator.js | 23 +++++++++-------------- validator.min.js | 2 +- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/isPostalCode.js b/lib/isPostalCode.js index ab04b2b44..ccf698edd 100644 --- a/lib/isPostalCode.js +++ b/lib/isPostalCode.js @@ -9,10 +9,15 @@ exports.default = function (str, locale) { if (locale in patterns) { return patterns[locale].test(str); } else if (locale === 'any') { - // Test each unique pattern - return !![].concat(_toConsumableArray(new Set(Object.values(patterns)))).find(function (pattern) { - return pattern.test(str); - }); + for (var key in patterns) { + if (patterns.hasOwnProperty(key)) { + var pattern = patterns[key]; + if (pattern.test(str)) { + return true; + } + } + } + return false; } throw new Error('Invalid locale \'' + locale + '\''); }; @@ -23,8 +28,6 @@ var _assertString2 = _interopRequireDefault(_assertString); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } - // common patterns var threeDigit = /^\d{3}$/; var fourDigit = /^\d{4}$/; diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index a38c8bab0..98bedf678 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -48,8 +48,15 @@ export default function (str, locale) { if (locale in patterns) { return patterns[locale].test(str); } else if (locale === 'any') { - // Test each unique pattern - return !![...new Set(Object.values(patterns))].find(pattern => pattern.test(str)); + for (const key in patterns) { + if (patterns.hasOwnProperty(key)) { + const pattern = patterns[key]; + if (pattern.test(str)) { + return true; + } + } + } + return false; } throw new Error(`Invalid locale '${locale}'`); } diff --git a/validator.js b/validator.js index fd2796c4a..88db469df 100644 --- a/validator.js +++ b/validator.js @@ -69,16 +69,6 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; -var toConsumableArray = function (arr) { - if (Array.isArray(arr)) { - for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; - - return arr2; - } else { - return Array.from(arr); - } -}; - function toString(input) { if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && input !== null) { if (typeof input.toString === 'function') { @@ -1123,10 +1113,15 @@ var isPostalCode = function (str, locale) { if (locale in patterns) { return patterns[locale].test(str); } else if (locale === 'any') { - // Test each unique pattern - return !![].concat(toConsumableArray(new Set(Object.values(patterns)))).find(function (pattern) { - return pattern.test(str); - }); + for (var key in patterns) { + if (patterns.hasOwnProperty(key)) { + var pattern = patterns[key]; + if (pattern.test(str)) { + return true; + } + } + } + return false; } throw new Error('Invalid locale \'' + locale + '\''); }; diff --git a/validator.min.js b/validator.min.js index d14a04053..9a58d71c7 100644 --- a/validator.min.js +++ b/validator.min.js @@ -20,4 +20,4 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,F)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!E.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!rt.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!ot.test(o))return!1;for(n=0;n<12;n++)i+=it[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function g(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function p(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,w=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,y=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,b=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,S=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,E=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,k=/^[0-9A-F]{1,4}$/i,D={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},Z=/^\[([^\]]+)\](?::([0-9]+))?$/,R=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,C={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},I={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},O=["AU","GB","HK","IN","NZ","ZA","ZM"],P=0;P=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,D);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,g=void 0,p=void 0;if(g=e.split("#"),e=g.shift(),g=e.split("?"),e=g.shift(),(g=e.split("://")).length>1){if(o=g.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(g[0]=e.substr(2))}if(""===(e=g.join("://")))return!1;if(g=e.split("/"),""===(e=g.shift())&&!r.require_host)return!0;if((g=e.split("@")).length>1&&(n=g.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,p=null;var h=(s=g.join("@")).match(Z);return h?(a="",p=h[1],f=h[2]||null):(a=(g=s.split(":")).shift(),g.length&&(f=g.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||p&&u(p,6))||(a=a||p,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),R.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in C)return C[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in I)return I[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),B.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),N.test(e)},isFullWidth:function(e){return t(e),j.test(e)},isHalfWidth:function(e){return t(e),T.test(e)},isVariableWidth:function(e){return t(e),j.test(e)&&T.test(e)},isMultibyte:function(e){return t(e),q.test(e)},isSurrogatePair:function(e){return t(e),H.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?K:M,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&G.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&W.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),X.test(e)},isISRC:function(e){return t(e),Y.test(e)},isMD5:function(e){return t(e),V.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=Q[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!tt.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!et.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=nt;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in lt)return lt[r].test(e);if("any"===r)return!!Object.values(lt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isPostalCode:function(e,r){if(t(e),r in mt)return mt[r].test(e);if("any"===r)return!![].concat($(new Set(Object.values(mt)))).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,at),g(r).test(e)},isISO8601:function(e){return t(e),ut.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||st.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),dt.test(e)},isLatLong:function(e){if(t(e),!e.includes(","))return!1;var r=e.split(",");return ct.test(r[0])&&ft.test(r[1])},ltrim:p,rtrim:h,trim:function(t,e){return h(p(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,_t),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~vt.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~$t.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~Ft.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.validator=e()}(this,function(){"use strict";function t(t){if(!("string"==typeof t||t instanceof String))throw new TypeError("This library (validator.js) validates strings only")}function e(e){return t(e),e=Date.parse(e),isNaN(e)?null:new Date(e)}function r(e){return t(e),parseFloat(e)}function o(t){return"object"===(void 0===t?"undefined":v(t))&&null!==t?t="function"==typeof t.toString?t.toString():"[object Object]":(null===t||void 0===t||isNaN(t)&&!t.length)&&(t=""),String(t)}function i(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=arguments[1];for(var r in e)void 0===t[r]&&(t[r]=e[r]);return t}function n(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=encodeURI(e).split(/%..|./).length-1;return n>=o&&(void 0===i||n<=i)}function l(e,r){t(e),(r=i(r,$)).allow_trailing_dot&&"."===e[e.length-1]&&(e=e.substring(0,e.length-1));var o=e.split(".");if(r.require_tld){var n=o.pop();if(!o.length||!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(n))return!1;if(/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(n))return!1}for(var l,a=0;a1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return u(e,4)||u(e,6);if("4"===r)return!!S.test(e)&&e.split(".").sort(function(t,e){return t-e})[3]<=255;if("6"===r){var o=e.split(":"),i=!1,n=u(o[o.length-1],4),l=n?7:8;if(o.length>l)return!1;if("::"===e)return!0;"::"===e.substr(0,2)?(o.shift(),o.shift(),i=!0):"::"===e.substr(e.length-2)&&(o.pop(),o.pop(),i=!0);for(var a=0;a0&&a=1:o.length===l}return!1}function s(t){return"[object RegExp]"===Object.prototype.toString.call(t)}function d(t,e){for(var r=0;r1&&void 0!==arguments[1]?arguments[1]:"";if(t(e),!(r=String(r)))return f(e,10)||f(e,13);var o=e.replace(/[\s-]+/g,""),i=0,n=void 0;if("10"===r){if(!et.test(o))return!1;for(n=0;n<9;n++)i+=(n+1)*o.charAt(n);if("X"===o.charAt(9)?i+=100:i+=10*o.charAt(9),i%11==0)return!!o}else if("13"===r){if(!rt.test(o))return!1;for(n=0;n<12;n++)i+=ot[n%2]*o.charAt(n);if(o.charAt(12)-(10-i%10)%10==0)return!!o}return!1}function p(t){var e="(\\"+t.symbol.replace(/\./g,"\\.")+")"+(t.require_symbol?"":"?"),r="("+["0","[1-9]\\d*","[1-9]\\d{0,2}(\\"+t.thousands_separator+"\\d{3})*"].join("|")+")?"+("(\\"+t.decimal_separator+"\\d{2})?");return t.allow_negatives&&!t.parens_for_negatives&&(t.negative_sign_after_digits?r+="-?":t.negative_sign_before_digits&&(r="-?"+r)),t.allow_negative_sign_placeholder?r="( (?!\\-))?"+r:t.allow_space_after_symbol?r=" ?"+r:t.allow_space_after_digits&&(r+="( (?!$))?"),t.symbol_after_digits?r+=e:r=e+r,t.allow_negatives&&(t.parens_for_negatives?r="(\\("+r+"\\)|"+r+")":t.negative_sign_before_digits||t.negative_sign_after_digits||(r="-?"+r)),new RegExp("^(?!-? )(?=.*\\d)"+r+"$")}function g(e,r){t(e);var o=r?new RegExp("^["+r+"]+","g"):/^\s+/g;return e.replace(o,"")}function h(e,r){t(e);for(var o=r?new RegExp("["+r+"]"):/\s/,i=e.length-1;i>=0&&o.test(e[i]);)i--;return i$/i,x=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i,w=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i,y=/^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i,b=/^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i,S=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,E=/^[0-9A-F]{1,4}$/i,k={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1},D=/^\[([^\]]+)\](?::([0-9]+))?$/,Z=/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,R={"en-US":/^[A-Z]+$/i,"cs-CZ":/^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[A-ZÆØÅ]+$/i,"de-DE":/^[A-ZÄÖÜß]+$/i,"es-ES":/^[A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"nl-NL":/^[A-ZÉËÏÓÖÜ]+$/i,"hu-HU":/^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"pl-PL":/^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[А-ЯЁ]+$/i,"sr-RS@latin":/^[A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},C={"en-US":/^[0-9A-Z]+$/i,"cs-CZ":/^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,"da-DK":/^[0-9A-ZÆØÅ]$/i,"de-DE":/^[0-9A-ZÄÖÜß]+$/i,"es-ES":/^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,"fr-FR":/^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,"hu-HU":/^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,"nl-NL":/^[0-9A-ZÉËÏÓÖÜ]+$/i,"pl-PL":/^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,"pt-PT":/^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,"ru-RU":/^[0-9А-ЯЁ]+$/i,"sr-RS@latin":/^[0-9A-ZČĆŽŠĐ]+$/i,"sr-RS":/^[0-9А-ЯЂЈЉЊЋЏ]+$/i,"tr-TR":/^[0-9A-ZÇĞİıÖŞÜ]+$/i,"uk-UA":/^[0-9А-ЩЬЮЯЄIЇҐ]+$/i,ar:/^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/},I=["AU","GB","HK","IN","NZ","ZA","ZM"],O=0;O=0},matches:function(e,r,o){return t(e),"[object RegExp]"!==Object.prototype.toString.call(r)&&(r=new RegExp(r,o)),r.test(e)},isEmail:a,isURL:function(e,r){if(t(e),!e||e.length>=2083||/[\s<>]/.test(e))return!1;if(0===e.indexOf("mailto:"))return!1;r=i(r,k);var o=void 0,n=void 0,a=void 0,s=void 0,c=void 0,f=void 0,p=void 0,g=void 0;if(p=e.split("#"),e=p.shift(),p=e.split("?"),e=p.shift(),(p=e.split("://")).length>1){if(o=p.shift(),r.require_valid_protocol&&-1===r.protocols.indexOf(o))return!1}else{if(r.require_protocol)return!1;r.allow_protocol_relative_urls&&"//"===e.substr(0,2)&&(p[0]=e.substr(2))}if(""===(e=p.join("://")))return!1;if(p=e.split("/"),""===(e=p.shift())&&!r.require_host)return!0;if((p=e.split("@")).length>1&&(n=p.shift()).indexOf(":")>=0&&n.split(":").length>2)return!1;f=null,g=null;var h=(s=p.join("@")).match(D);return h?(a="",g=h[1],f=h[2]||null):(a=(p=s.split(":")).shift(),p.length&&(f=p.join(":"))),!(null!==f&&(c=parseInt(f,10),!/^[0-9]+$/.test(f)||c<=0||c>65535)||!(u(a)||l(a,r)||g&&u(g,6))||(a=a||g,r.host_whitelist&&!d(a,r.host_whitelist)||r.host_blacklist&&d(a,r.host_blacklist)))},isMACAddress:function(e){return t(e),Z.test(e)},isIP:u,isFQDN:l,isBoolean:function(e){return t(e),["true","false","1","0"].indexOf(e)>=0},isAlpha:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in R)return R[r].test(e);throw new Error("Invalid locale '"+r+"'")},isAlphanumeric:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"en-US";if(t(e),r in C)return C[r].test(e);throw new Error("Invalid locale '"+r+"'")},isNumeric:function(e){return t(e),z.test(e)},isLowercase:function(e){return t(e),e===e.toLowerCase()},isUppercase:function(e){return t(e),e===e.toUpperCase()},isAscii:function(e){return t(e),B.test(e)},isFullWidth:function(e){return t(e),N.test(e)},isHalfWidth:function(e){return t(e),j.test(e)},isVariableWidth:function(e){return t(e),N.test(e)&&j.test(e)},isMultibyte:function(e){return t(e),T.test(e)},isSurrogatePair:function(e){return t(e),q.test(e)},isInt:function(e,r){t(e);var o=(r=r||{}).hasOwnProperty("allow_leading_zeroes")&&!r.allow_leading_zeroes?H:K,i=!r.hasOwnProperty("min")||e>=r.min,n=!r.hasOwnProperty("max")||e<=r.max,l=!r.hasOwnProperty("lt")||er.gt;return o.test(e)&&i&&n&&l&&a},isFloat:function(e,r){return t(e),r=r||{},""!==e&&"."!==e&&M.test(e)&&(!r.hasOwnProperty("min")||e>=r.min)&&(!r.hasOwnProperty("max")||e<=r.max)&&(!r.hasOwnProperty("lt")||er.gt)},isDecimal:function(e){return t(e),""!==e&&G.test(e)},isHexadecimal:c,isDivisibleBy:function(e,o){return t(e),r(e)%parseInt(o,10)==0},isHexColor:function(e){return t(e),J.test(e)},isISRC:function(e){return t(e),X.test(e)},isMD5:function(e){return t(e),Y.test(e)},isJSON:function(e){t(e);try{var r=JSON.parse(e);return!!r&&"object"===(void 0===r?"undefined":v(r))}catch(t){}return!1},isEmpty:function(e){return t(e),0===e.length},isLength:function(e,r){t(e);var o=void 0,i=void 0;"object"===(void 0===r?"undefined":v(r))?(o=r.min||0,i=r.max):(o=arguments[1],i=arguments[2]);var n=e.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)||[],l=e.length-n.length;return l>=o&&(void 0===i||l<=i)},isByteLength:n,isUUID:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"all";t(e);var o=V[r];return o&&o.test(e)},isMongoId:function(e){return t(e),c(e)&&24===e.length},isAfter:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n>i)},isBefore:function(r){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:String(new Date);t(r);var i=e(o),n=e(r);return!!(n&&i&&n=0}return"object"===(void 0===r?"undefined":v(r))?r.hasOwnProperty(e):!(!r||"function"!=typeof r.indexOf)&&r.indexOf(e)>=0},isCreditCard:function(e){t(e);var r=e.replace(/[- ]+/g,"");if(!Q.test(r))return!1;for(var o=0,i=void 0,n=void 0,l=void 0,a=r.length-1;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n%10+1:n,l=!l;return!(o%10!=0||!r)},isISIN:function(e){if(t(e),!tt.test(e))return!1;for(var r=e.replace(/[A-Z]/g,function(t){return parseInt(t,36)}),o=0,i=void 0,n=void 0,l=!0,a=r.length-2;a>=0;a--)i=r.substring(a,a+1),n=parseInt(i,10),o+=l&&(n*=2)>=10?n+1:n,l=!l;return parseInt(e.substr(e.length-1),10)===(1e4-o)%10},isISBN:f,isISSN:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(e);var o=it;if(o=r.require_hyphen?o.replace("?",""):o,!(o=r.case_sensitive?new RegExp(o):new RegExp(o,"i")).test(e))return!1;var i=e.replace("-",""),n=8,l=0,a=!0,u=!1,s=void 0;try{for(var d,c=i[Symbol.iterator]();!(a=(d=c.next()).done);a=!0){var f=d.value;l+=("X"===f.toUpperCase()?10:+f)*n,--n}}catch(t){u=!0,s=t}finally{try{!a&&c.return&&c.return()}finally{if(u)throw s}}return l%11==0},isMobilePhone:function(e,r){if(t(e),r in nt)return nt[r].test(e);if("any"===r)return!!Object.values(nt).find(function(t){return t.test(e)});throw new Error("Invalid locale '"+r+"'")},isPostalCode:function(e,r){if(t(e),r in ht)return ht[r].test(e);if("any"===r){for(var o in ht)if(ht.hasOwnProperty(o)&&ht[o].test(e))return!0;return!1}throw new Error("Invalid locale '"+r+"'")},isCurrency:function(e,r){return t(e),r=i(r,lt),p(r).test(e)},isISO8601:function(e){return t(e),at.test(e)},isBase64:function(e){t(e);var r=e.length;if(!r||r%4!=0||ut.test(e))return!1;var o=e.indexOf("=");return-1===o||o===r-1||o===r-2&&"="===e[r-1]},isDataURI:function(e){return t(e),st.test(e)},isLatLong:function(e){if(t(e),!e.includes(","))return!1;var r=e.split(",");return dt.test(r[0])&&ct.test(r[1])},ltrim:g,rtrim:h,trim:function(t,e){return h(g(t,e),e)},escape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(//g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},unescape:function(e){return t(e),e.replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">").replace(///g,"/").replace(/`/g,"`")},stripLow:function(e,r){return t(e),m(e,r?"\\x00-\\x09\\x0B\\x0C\\x0E-\\x1F\\x7F":"\\x00-\\x1F\\x7F")},whitelist:function(e,r){return t(e),e.replace(new RegExp("[^"+r+"]+","g"),"")},blacklist:m,isWhitelisted:function(e,r){t(e);for(var o=e.length-1;o>=0;o--)if(-1===r.indexOf(e[o]))return!1;return!0},normalizeEmail:function(t,e){if(e=i(e,mt),!a(t))return!1;var r=t.split("@"),o=r.pop(),n=[r.join("@"),o];if(n[1]=n[1].toLowerCase(),"gmail.com"===n[1]||"googlemail.com"===n[1]){if(e.gmail_remove_subaddress&&(n[0]=n[0].split("+")[0]),e.gmail_remove_dots&&(n[0]=n[0].replace(/\./g,"")),!n[0].length)return!1;(e.all_lowercase||e.gmail_lowercase)&&(n[0]=n[0].toLowerCase()),n[1]=e.gmail_convert_googlemaildotcom?"gmail.com":n[1]}else if(~_t.indexOf(n[1])){if(e.icloud_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.icloud_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~vt.indexOf(n[1])){if(e.outlookdotcom_remove_subaddress&&(n[0]=n[0].split("+")[0]),!n[0].length)return!1;(e.all_lowercase||e.outlookdotcom_lowercase)&&(n[0]=n[0].toLowerCase())}else if(~$t.indexOf(n[1])){if(e.yahoo_remove_subaddress){var l=n[0].split("-");n[0]=l.length>1?l.slice(0,-1).join("-"):l[0]}if(!n[0].length)return!1;(e.all_lowercase||e.yahoo_lowercase)&&(n[0]=n[0].toLowerCase())}else e.all_lowercase&&(n[0]=n[0].toLowerCase());return n.join("@")},toString:o}}); \ No newline at end of file