UNPKG

valienv

Version:

A simple environment variables validator for Node.js, web browsers and React Native

3 lines (2 loc) 1.5 kB
class e extends Error{constructor(t){super("Some environment variables cannot be validated: "+t.join(", ")),this.variables=void 0,Object.setPrototypeOf(this,e.prototype),this.name=this.constructor.name,this.variables=t}}const t=t=>{let{env:n,validators:r,overrides:o={}}=t;const i={},s=[],c=Object.keys(r),u=Object.keys(o).filter(e=>-1!==c.indexOf(e));if(u.forEach(e=>{i[e]=o[e]}),c.forEach(e=>{if(-1!==u.indexOf(e))return;const t=r[e];if(void 0!==t){const r=t(n[e]);void 0!==r?i[e]=r:s.push(e)}}),s.length>0){const t=new e(s);if("undefined"==typeof process)throw t;console.error(t.message),process.exit(1)}return i},n=function(e){return void 0===e&&(e=""),"true"===e||"1"===e||"false"!==e&&"0"!==e&&void 0},r=/^[^@\s]+@[^@\s]+\.[^@\s]+$/,o=function(e){if(void 0===e&&(e=""),r.test(e))return e},i=function(e){void 0===e&&(e="");const t=Number.parseFloat(e);if(!Number.isNaN(t))return t},s=function(e){void 0===e&&(e="");const t=Number.parseFloat(e);if(!Number.isNaN(t)&&t%1==0&&t>0&&t<65536)return t},c=function(e){if(void 0===e&&(e=""),""!==e)return e},u=function(e){void 0===e&&(e="");try{return new URL(e),e}catch(e){}},f=function(){var e=arguments;return function(t){void 0===t&&(t="");const n=[].slice.call(e).find(e=>e===t);if(void 0!==n)return n}},a=e=>t=>{const n=e(t);return void 0!==n?{defined:!0,value:n}:{defined:!1}};export{e as EnvValidationError,n as boolean,o as email,i as number,f as oneOf,a as optional,s as port,c as string,u as url,t as validate}; //# sourceMappingURL=index.mjs.map