Comparing version
@@ -1,2 +0,2 @@ | ||
function t(e){return t=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},t(e)}function e(t,n){return e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},e(t,n)}function n(t,r,o){return n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}()?Reflect.construct.bind():function(t,n,r){var o=[null];o.push.apply(o,n);var i=new(Function.bind.apply(t,o));return r&&e(i,r.prototype),i},n.apply(null,arguments)}function r(o){var i="function"==typeof Map?new Map:void 0;return r=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(r))return i.get(r);i.set(r,o)}function o(){return n(r,arguments,t(this).constructor)}return o.prototype=Object.create(r.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),e(o,r)},r(o)}var o=/*#__PURE__*/function(t){var n,r;function o(e){var n;return(n=t.call(this,"Some environment variables cannot be validated: "+e.join(", "))||this).variables=void 0,Object.setPrototypeOf(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n),o.prototype),n.name=n.constructor.name,n.variables=e,n}return r=t,(n=o).prototype=Object.create(r.prototype),n.prototype.constructor=n,e(n,r),o}(/*#__PURE__*/r(Error));exports.EnvValidationError=o,exports.boolean=function(t){return void 0===t&&(t=""),"true"===t||"false"!==t&&void 0},exports.number=function(t){void 0===t&&(t="");var e=Number.parseFloat(t);if(!Number.isNaN(e))return e},exports.oneOf=function(){var t=arguments;return function(e){void 0===e&&(e="");var n=[].slice.call(t).find(function(t){return t===e});if(void 0!==n)return n}},exports.optional=function(t){return function(e){var n=t(e);return void 0!==n?{defined:!0,value:n}:{defined:!1}}},exports.string=function(t){if(void 0===t&&(t=""),""!==t)return t},exports.validate=function(t){var e=t.env,n=t.validators,r=t.overrides,i=void 0===r?{}:r,u={},c=[],f=Object.keys(n),a=Object.keys(i).filter(function(t){return-1!==f.indexOf(t)});if(a.forEach(function(t){u[t]=i[t]}),f.forEach(function(t){if(-1===a.indexOf(t)){var r=n[t];if(void 0!==r){var o=r(e[t]);void 0!==o?u[t]=o:c.push(t)}}}),c.length>0)throw new o(c);return u}; | ||
class t extends Error{constructor(e){super("Some environment variables cannot be validated: "+e.join(", ")),this.variables=void 0,Object.setPrototypeOf(this,t.prototype),this.name=this.constructor.name,this.variables=e}}const e=/^[^@\s]+@[^@\s]+\.[^@\s]+$/;exports.EnvValidationError=t,exports.boolean=function(t){return void 0===t&&(t=""),"true"===t||"1"===t||"false"!==t&&"0"!==t&&void 0},exports.email=function(t){if(void 0===t&&(t=""),e.test(t))return t},exports.number=function(t){void 0===t&&(t="");const e=Number.parseFloat(t);if(!Number.isNaN(e))return e},exports.oneOf=function(){var t=arguments;return function(e){void 0===e&&(e="");const r=[].slice.call(t).find(t=>t===e);if(void 0!==r)return r}},exports.optional=t=>e=>{const r=t(e);return void 0!==r?{defined:!0,value:r}:{defined:!1}},exports.port=function(t){void 0===t&&(t="");const e=Number.parseFloat(t);if(!Number.isNaN(e)&&e%1==0&&e>0&&e<65536)return e},exports.string=function(t){if(void 0===t&&(t=""),""!==t)return t},exports.url=function(t){void 0===t&&(t="");try{return new URL(t),t}catch{}},exports.validate=e=>{let{env:r,validators:o,overrides:n={}}=e;const i={},s=[],a=Object.keys(o),c=Object.keys(n).filter(t=>-1!==a.indexOf(t));if(c.forEach(t=>{i[t]=n[t]}),a.forEach(t=>{if(-1!==c.indexOf(t))return;const e=o[t];if(void 0!==e){const o=e(r[t]);void 0!==o?i[t]=o:s.push(t)}}),s.length>0)throw new t(s);return i}; | ||
//# sourceMappingURL=index.js.map |
@@ -9,5 +9,8 @@ export type Validator<T> = (value: string | undefined) => T | undefined; | ||
export declare const boolean: Validator<boolean>; | ||
export declare const email: Validator<string>; | ||
export declare const number: Validator<number>; | ||
export declare const port: Validator<number>; | ||
export declare const string: Validator<string>; | ||
export declare const url: Validator<string>; | ||
export declare const oneOf: <T extends string>(...values: readonly T[]) => Validator<T>; | ||
export declare const optional: <T>(validator: Validator<T>) => Validator<OptionalEnvValue<T>>; |
{ | ||
"name": "valienv", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"license": "MIT", | ||
@@ -44,15 +44,17 @@ "description": "A simple environment variables validator for Node.js, web browsers and React Native", | ||
"prettier": { | ||
"trailingComma": "all" | ||
"plugins": [ | ||
"prettier-plugin-organize-imports" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.2.5", | ||
"@typescript-eslint/eslint-plugin": "^5.59.9", | ||
"@typescript-eslint/parser": "^5.59.9", | ||
"eslint": "^8.42.0", | ||
"@types/node": "^20.11.20", | ||
"@typescript-eslint/eslint-plugin": "^7.0.2", | ||
"@typescript-eslint/parser": "^7.0.2", | ||
"eslint": "^8.56.0", | ||
"microbundle": "^0.15.1", | ||
"prettier": "^2.8.8", | ||
"prettier-plugin-organize-imports": "^3.2.2", | ||
"typescript": "^5.1.3", | ||
"vitest": "^0.32.0" | ||
"prettier": "^3.2.5", | ||
"prettier-plugin-organize-imports": "^3.2.4", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.3.1" | ||
} | ||
} |
@@ -83,3 +83,3 @@ # ✓ valienv | ||
By default, `valienv` only exports 3 validators: `string`, `number` and `boolean`. It also offers `oneOf`, a helper to create validators for union of string literals. | ||
By default, `valienv` exports 6 validators: `string`, `number`, `boolean`, `url`, `port` and `email`. It also offers `oneOf`, a helper to create validators for union of string literals. | ||
@@ -93,9 +93,7 @@ It's very easy to write your own: | ||
// returns the result in case of valid value: | ||
const port: Validator<number> = ( | ||
value: string | undefined = "", | ||
): number | undefined => { | ||
const number = Number.parseInt(value); | ||
const buffer: Validator<Buffer> = (value: string = "") => { | ||
const valid = /^[A-F\d]+$/i.test(value); | ||
if (number > 0 && number < 65536) { | ||
return number; | ||
if (valid) { | ||
return Buffer.from(value); | ||
} | ||
@@ -105,3 +103,3 @@ }; | ||
// with process.env = { | ||
// PORT: "3000", | ||
// COOKIE_KEY: "aba4a6fb2222ef28d81e4be445a51fba", | ||
// } | ||
@@ -112,7 +110,7 @@ | ||
validators: { | ||
PORT: port, | ||
COOKIE_KEY: buffer, | ||
}, | ||
}); | ||
// -> typeof env = Readonly<{ PORT: number }> | ||
// -> typeof env = Readonly<{ COOKIE_KEY: Buffer }> | ||
``` | ||
@@ -119,0 +117,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23554
-3.38%42
-10.64%203
-0.98%