UNPKG

@mdaemon/validate

Version:

A validation library used by MDaemon Remote Administration

2 lines (1 loc) 10.2 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).validate={})}(this,(function(e){"use strict";function r(e){return"string"==typeof e&&/\u000d|\u000a/g.test(e)}function t(e){return"string"==typeof e&&/\u0008|\u000c|\u0009|\u000b/g.test(e)||r(e)}function n(e){return"string"==typeof e&&!!e.length&&!t(e)&&/^[A-Za-z\-]+$/g.test(e)&&!/(^-)|(-$)/.test(e)}function i(e){return"string"==typeof e&&!!e.length&&!t(e)}function a(e){if("string"!=typeof e||-1===e.indexOf(":"))return!1;var r=e.substring(0,e.indexOf(":")).trim(),t=e.substring(e.indexOf(":")+1).trim();return n(r)&&i(t)}function o(e,r){if("string"!=typeof e)return!1;if(r&&(-1!==e.indexOf("*")||-1!==e.indexOf("?")))return/[\S]+@[\S]+/.test(e);return/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\"[^\"\\]+\"))@((\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(e.trim())}function s(e,r){return void 0===r&&(r=!1),"string"==typeof e&&(!t(e)&&(!/[()<>\[\]:;@\ ]/gm.test(e)&&(r?/^[0-9a-zA-Z.?\-*#]+\.[0-9a-zA-Z.?\-*#]+$/g.test(e):/^[0-9a-zA-Z.\-]+\.[0-9a-zA-Z.\-]+$/g.test(e))))}function u(e,r){if(void 0===r&&(r=!1),"string"!=typeof e)return!1;var t=-1===e.indexOf(".")&&-1!==e.indexOf(":");if(r){var n=e.indexOf("*");if(-1===n&&(n=e.indexOf("#")),-1===n&&(n=e.indexOf("?")),t){if(-1!==n)return-1!==(n=e.indexOf(":"))&&(-1!==(n=e.indexOf(":",n+1))&&-1!==(n=e.indexOf(":",n+1)))}else{if(-1!==n)return-1!==(n=e.indexOf("."))&&(-1!==(n=e.indexOf(".",n+1))&&-1!==(n=e.indexOf(".",n+1)));if(-1!==(n=e.indexOf("/"))&&-1===e.indexOf("."))return-1!==(n=e.indexOf(":"))&&-1!==(n=e.indexOf(":",n))}}var i=/\d/;return!!i.test(e)&&(t&&/\/\d{1,3}$/.test(e)&&(e=e.substr(0,e.lastIndexOf("/"))),(i=/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$|^(((2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)\.){3}(2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?)(\/(3[012]|[12]?[0-9])))$/).test(e))}function d(e){return!("string"!=typeof e||e.length<1)&&/^\d*$/.test(e)}function f(e){if("string"!=typeof e)return!1;return"string"==typeof e&&!!e.trim()&&!r(e)&&!/["*:<>?\/\\|]+/.test(e)}function p(e,t){if(void 0===t&&(t=!1),"string"!=typeof e)return!1;var n=t?/^([A-Za-z]{1}:\\|\\\\)([^":<>\/\\|]+\\?)+([^":<>\/\\|]+)$/:/^([A-Za-z]{1}:\\|\\\\)([^"*:<>?\/\\|]+\\?)+([^"*:<>?\/\\|]+)$/;return"string"==typeof e&&!!e.trim()&&!r(e)&&n.test(e)}function m(e){if("string"!=typeof e)return!1;return"string"==typeof e&&!!e.trim()&&!r(e)&&/^(((CN|2\.5\.4\.3|UID|0\.9\.2342\.19200300\.100\.1\.1) *= *([^,]*)))?( *,? *(((?:CN|2\.5\.4\.3|OU|2\.5\.4\.11) *= *[^,]+,?)+))?( *,? *((DC|0\.9\.2342\.19200300\.100\.1\.25) *= *[^,]+)+)*$/i.test(e)}function c(e){return"string"==typeof e&&/^\+?[0-9)( -]{5,20}$/.test(e)}function l(e){return"string"==typeof e&&/[A-Z]+/.test(e)}function h(e){return"string"==typeof e&&/[a-z]+/.test(e)}function g(e){return"string"==typeof e&&/[0-9]+/.test(e)}function y(e){return"string"==typeof e&&/[!-/]+|[:-@]+|[[-`]+|[{-~]/.test(e)}var v={requireUpper:!1,requireLower:!1,requireNumber:!1,requireSpecial:!1,requireMinLength:-1,requireMaxLength:-1,setByUser:!1};function x(e){return void 0!==e.upper&&(v.requireUpper=!!e.upper),void 0!==e.lower&&(v.requireLower=!!e.lower),void 0!==e.special&&(v.requireSpecial=!!e.special),void 0!==e.number&&(v.requireNumber=!!e.number),"number"==typeof e.min&&e.min>0&&(v.requireMinLength=e.min),"number"==typeof e.max&&e.max>0&&(v.requireMaxLength=e.max),v.requireMaxLength>0&&v.requireMinLength>0&&v.requireMaxLength<=v.requireMinLength?(console.error("The minimum password length requirement must be less than the maximum password length requirement"),!1):(v.setByUser=!0,!0)}function b(){v.requireUpper=!1,v.requireLower=!1,v.requireNumber=!1,v.requireSpecial=!1,v.requireMinLength=-1,v.requireMaxLength=-1,v.setByUser=!1}function A(e,r,t,n){var i={};if("string"!=typeof e)return i={special:!1,lower:!1,upper:!1,number:!1,length:0},("number"==typeof t||v.setByUser&&v.requireMinLength>0)&&(i.min=!1),("number"==typeof n||v.setByUser&&v.requireMaxLength>0)&&(i.max=!1),i;if(v.setByUser){var a=Object.assign(v);return(a.requireSpecial&&void 0===r||r)&&(i.special=y(e)),a.requireMinLength>0&&"number"!=typeof t?i.min=e.length>=a.requireMinLength:"number"==typeof t&&t>0&&(i.min=e.length>=t),a.requireMaxLength>0&&"number"!=typeof n?i.max=e.length<=a.requireMaxLength:"number"==typeof n&&n>0&&(i.max=e.length<=n),a.requireLower&&(i.lower=h(e)),a.requireUpper&&(i.upper=l(e)),a.requireNumber&&(i.number=g(e)),i}return i={special:!r||y(e),lower:h(e),upper:l(e),number:g(e),length:e.length},"number"==typeof t&&(i.min=e.length>=t),"number"==typeof n&&(i.max=e.length<=n),i}function q(e,r,t,n){if("string"!=typeof e)return!1;var i=A(e,r,t,n);return!(void 0!==i.lower&&!i.lower)&&(!(void 0!==i.upper&&!i.upper)&&(!(void 0!==i.number&&!i.number)&&(!(void 0!==i.special&&!i.special)&&(!(void 0!==i.min&&!i.min)&&!(void 0!==i.max&&!i.max)))))}var w=new Map;function L(e){if(!e)return e;if("function"==typeof e)return e;if(Array.isArray(e))return e.map((function(e){return L(e)}));if("object"==typeof e){var r={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=L(e[t]));return r}return e}function O(e){return L(w.get(e))}function M(e,r){if(!e||"string"!=typeof e)throw new TypeError("A string is required for the schema name");w.set(e,L(r))}function F(e,r,t){void 0===t&&(t=0);var n={valid:!0,errors:[]};if(e.type){var i=typeof r;if("number"===e.type&&"number"!==i?(n.valid=!1,n.errors.push("Expected number, got ".concat(i))):"string"===e.type&&"string"!==i?(n.valid=!1,n.errors.push("Expected string, got ".concat(i))):"boolean"===e.type&&"boolean"!==i?(n.valid=!1,n.errors.push("Expected boolean, got ".concat(i))):"array"!==e.type||Array.isArray(r)?"object"===e.type&&"object"!==i&&(n.valid=!1,n.errors.push("Expected object, got ".concat(i))):(n.valid=!1,n.errors.push("Expected array, got ".concat(i))),"array"===e.type&&Array.isArray(r))"object"==typeof e.arraySchema&&t<10&&r.forEach((function(r,i){var a=F(e.arraySchema,r,t+1);a.valid||(n.valid=!1,a.errors.forEach((function(e){n.errors.push("[".concat(i,"]: ").concat(e))})))}));else if("object"===e.type&&"object"===i&&e.properties&&t<10){var a=function(i){var a=F(e.properties[i],r[i],t+1);a.valid||(n.valid=!1,a.errors.forEach((function(e){n.errors.push("".concat(i,": ").concat(e))})))};for(var o in e.properties)a(o)}}return e.required&&null==r&&(n.valid=!1,n.errors.push("Value is required")),"number"==typeof e.minLength&&"string"==typeof r&&r.length<e.minLength&&(n.valid=!1,n.errors.push("Minimum length is ".concat(e.minLength))),"number"==typeof e.maxLength&&"string"==typeof r&&r.length>e.maxLength&&(n.valid=!1,n.errors.push("Maximum length is ".concat(e.maxLength))),"number"==typeof e.minimum&&"number"==typeof r&&r<e.minimum&&(n.valid=!1,n.errors.push("Minimum value is ".concat(e.minimum))),"number"==typeof e.maximum&&"number"==typeof r&&r>e.maximum&&(n.valid=!1,n.errors.push("Maximum value is ".concat(e.maximum))),"number"==typeof e.minItems&&Array.isArray(r)&&r.length<e.minItems&&(n.valid=!1,n.errors.push("Minimum length is ".concat(e.minItems))),"number"==typeof e.maxItems&&Array.isArray(r)&&r.length>e.maxItems&&(n.valid=!1,n.errors.push("Maximum length is ".concat(e.maxItems))),Array.isArray(e.options)&&e.options.length&&!e.options.includes(r)&&(n.valid=!1,n.errors.push("Invalid value, expected one of: ".concat(e.options.join(", ")))),"function"!=typeof e.validate||e.validate(r)||(n.valid=!1,n.errors.push("Custom validation failed")),"string"!=typeof e.pattern||"string"!=typeof r||new RegExp(e.pattern).test(r)||(n.valid=!1,n.errors.push("Value does not match pattern ".concat(e.pattern))),n}function z(e){return function(r){var t=[],n=F(w.get(e),r);return n.valid||t.push.apply(t,n.errors),{valid:0===t.length,errors:t}}}function Z(e,r){if(!e||"string"!=typeof e)throw new TypeError("A string is required for the schema name");if(!r||"object"!=typeof r)throw new TypeError("A JSON object is required for the schema");return M(e,r),z(e)}var N={domain:s,email:o,createSchemaValidator:Z,getSchema:O,updateSchema:M,hasControlCharacters:t,hasLowerCase:h,hasNumber:g,hasSpecial:y,hasUpperCase:l,header:a,headerName:n,headerValue:i,isInt:d,ldapDN:m,ip:u,password:A,isValidPassword:q,phoneNumber:c,resetPasswordRequirements:b,setPasswordRequirements:x,windowsFileName:f,windowsPath:p};e.createSchemaValidator=Z,e.default=N,e.getSchema=O,e.hasControlCharacters=t,e.hasLowerCase=h,e.hasNumber=g,e.hasSpecial=y,e.hasUpperCase=l,e.isValidPassword=q,e.resetPasswordRequirements=b,e.setPasswordRequirements=x,e.updateSchema=M,e.validateDomain=s,e.validateEmailAddress=o,e.validateHeader=a,e.validateHeaderName=n,e.validateHeaderValue=i,e.validateIPAddress=u,e.validateInt=d,e.validateLdapDN=m,e.validatePassword=A,e.validatePhoneNumber=c,e.validateWindowsFileName=f,e.validateWindowsPath=p,Object.defineProperty(e,"__esModule",{value:!0})}));