UNPKG

@mdaemon/validate

Version:

A validation library used by MDaemon Remote Administration

2 lines (1 loc) 10.4 kB
"use strict";function e(e){return"string"==typeof e&&/\u000d|\u000a/g.test(e)}function r(r){return"string"==typeof r&&/\u0008|\u000c|\u0009|\u000b/g.test(r)||e(r)}function t(e){return"string"==typeof e&&!!e.length&&!r(e)&&/^[A-Za-z\-]+$/g.test(e)&&!/(^-)|(-$)/.test(e)}function n(e){return"string"==typeof e&&!!e.length&&!r(e)}function i(e){if("string"!=typeof e||-1===e.indexOf(":"))return!1;var r=e.substring(0,e.indexOf(":")).trim(),i=e.substring(e.indexOf(":")+1).trim();return t(r)&&n(i)}function a(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 o(e,t){return void 0===t&&(t=!1),"string"==typeof e&&(!r(e)&&(!/[()<>\[\]:;@\ ]/gm.test(e)&&(t?/^[0-9a-zA-Z.?\-*#]+\.[0-9a-zA-Z.?\-*#]+$/g.test(e):/^[0-9a-zA-Z.\-]+\.[0-9a-zA-Z.\-]+$/g.test(e))))}function s(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.substring(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 u(e){return s(e,!1)&&-1!==e.indexOf(".")&&-1===e.indexOf(":")}function d(e){return s(e,!1)&&-1!==e.indexOf(":")&&-1===e.indexOf(".")}function p(e){return!("string"!=typeof e||e.length<1)&&/^\d*$/.test(e)}function f(r){if("string"!=typeof r)return!1;return"string"==typeof r&&!!r.trim()&&!e(r)&&!/["*:<>?\/\\|]+/.test(r)}function m(r,t){if(void 0===t&&(t=!1),"string"!=typeof r)return!1;var n=t?/^([A-Za-z]{1}:\\|\\\\)([^":<>\/\\|]+\\?)+([^":<>\/\\|]+)$/:/^([A-Za-z]{1}:\\|\\\\)([^"*:<>?\/\\|]+\\?)+([^"*:<>?\/\\|]+)$/;return"string"==typeof r&&!!r.trim()&&!e(r)&&n.test(r)}function c(r){if("string"!=typeof r)return!1;return"string"==typeof r&&!!r.trim()&&!e(r)&&/^(((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(r)}function l(e){return"string"==typeof e&&/^\+?[0-9)( -]{5,20}$/.test(e)}function h(e){return"string"==typeof e&&/[A-Z]+/.test(e)}function g(e){return"string"==typeof e&&/[a-z]+/.test(e)}function y(e){return"string"==typeof e&&/[0-9]+/.test(e)}function x(e){return"string"==typeof e&&/[!-/]+|[:-@]+|[[-`]+|[{-~]/.test(e)}Object.defineProperty(exports,"__esModule",{value:!0});var v={requireUpper:!1,requireLower:!1,requireNumber:!1,requireSpecial:!1,requireMinLength:-1,requireMaxLength:-1,setByUser:!1};function b(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 A(){v.requireUpper=!1,v.requireLower=!1,v.requireNumber=!1,v.requireSpecial=!1,v.requireMinLength=-1,v.requireMaxLength=-1,v.setByUser=!1}function q(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=x(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=g(e)),a.requireUpper&&(i.upper=h(e)),a.requireNumber&&(i.number=y(e)),i}return i={special:!r||x(e),lower:g(e),upper:h(e),number:y(e),length:e.length},"number"==typeof t&&(i.min=e.length>=t),"number"==typeof n&&(i.max=e.length<=n),i}function w(e,r,t,n){if("string"!=typeof e)return!1;var i=q(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 O=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 M(e){return L(O.get(e))}function F(e,r){if(!e||"string"!=typeof e)throw new TypeError("A string is required for the schema name");O.set(e,L(r))}function z(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=z(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=z(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=z(O.get(e),r);return n.valid||t.push.apply(t,n.errors),{valid:0===t.length,errors:t}}}function N(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 F(e,r),Z(e)}var S={domain:o,email:a,createSchemaValidator:N,getSchema:M,updateSchema:F,hasControlCharacters:r,hasLowerCase:g,hasNumber:y,hasSpecial:x,hasUpperCase:h,header:i,headerName:t,headerValue:n,isInt:p,ldapDN:c,ip:s,ipv4:u,ipv6:d,password:q,isValidPassword:w,phoneNumber:l,resetPasswordRequirements:A,setPasswordRequirements:b,windowsFileName:f,windowsPath:m};exports.createSchemaValidator=N,exports.default=S,exports.getSchema=M,exports.hasControlCharacters=r,exports.hasLowerCase=g,exports.hasNumber=y,exports.hasSpecial=x,exports.hasUpperCase=h,exports.isValidPassword=w,exports.resetPasswordRequirements=A,exports.setPasswordRequirements=b,exports.updateSchema=F,exports.validateDomain=o,exports.validateEmailAddress=a,exports.validateHeader=i,exports.validateHeaderName=t,exports.validateHeaderValue=n,exports.validateIPAddress=s,exports.validateIPv4=u,exports.validateIPv6=d,exports.validateInt=p,exports.validateLdapDN=c,exports.validatePassword=q,exports.validatePhoneNumber=l,exports.validateWindowsFileName=f,exports.validateWindowsPath=m;