🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

valienv

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

valienv - npm Package Compare versions

Comparing version

to
0.5.0

8

dist/errors.d.ts
export declare class EnvValidationError extends Error {
invalidVariables: string[];
missingVariables: string[];
constructor({ invalidVariables, missingVariables, }: {
invalidVariables?: string[];
missingVariables?: string[];
});
variables: string[];
constructor(variables: string[]);
}

@@ -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,r=e.invalidVariables,i=void 0===r?[]:r,u=e.missingVariables,a=void 0===u?[]:u,c=["Some environment variables cannot be validated"];return i.length>0&&c.push("Invalid variables: "+i.join(", ")),a.length>0&&c.push("Missing variables: "+a.join(", ")),(n=t.call(this,c.join("\n"))||this).invalidVariables=void 0,n.missingVariables=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.invalidVariables=i,n.missingVariables=a,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"true"===t||"false"!==t&&void 0},exports.number=function(t){var e=parseFloat(t);if(!Number.isNaN(e))return e},exports.oneOf=function(){var t=arguments;return function(e){var n=[].slice.call(t).find(function(t){return t===e});if(void 0!==n)return n}},exports.string=function(t){return t},exports.validate=function(t){var e=t.env,n=t.validators,r=t.overrides,i=void 0===r?{}:r,u={},a=[],c=[],f=Object.keys(n),s=Object.keys(i).filter(function(t){return-1!==f.indexOf(t)});if(s.forEach(function(t){u[t]=i[t]}),f.forEach(function(t){if(-1===s.indexOf(t)){var r=n[t],o=e[t];if(void 0!==o){if(void 0!==r){var i=r(String(o));void 0!==i?u[t]=i:a.push(t)}}else c.push(t)}}),a.length>0||c.length>0)throw new o({invalidVariables:a,missingVariables:c});return u};
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};
//# sourceMappingURL=index.js.map
import { Validator } from "./validators";
export declare const validate: <Validators extends Record<string, Validator<unknown>>>({ env, validators, overrides, }: {
env: Record<string, string | number | boolean | undefined>;
env: Record<string, string | undefined>;
validators: Validators;
overrides?: { [Key in keyof Validators]?: ReturnType<Validators[Key]> | undefined; } | undefined;
}) => Readonly<{ [Key_1 in keyof Validators]: Exclude<ReturnType<Validators[Key_1]>, undefined>; }>;

@@ -1,2 +0,8 @@

export type Validator<T> = (value: string) => T | undefined;
export type Validator<T> = (value: string | undefined) => T | undefined;
export type OptionalEnvValue<T> = {
defined: true;
value: T;
} | {
defined: false;
};
export declare const boolean: Validator<boolean>;

@@ -6,1 +12,2 @@ export declare const number: Validator<number>;

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.4.0",
"version": "0.5.0",
"license": "MIT",
"description": "A simple environment variables validator for Node.js and web browsers.",
"description": "A simple environment variables validator for Node.js, web browsers and React Native",
"author": "Mathieu Acthernoene <zoontek@gmail.com>",

@@ -47,12 +47,12 @@ "homepage": "https://github.com/zoontek/valienv#readme",

"devDependencies": {
"@types/node": "^18.15.10",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
"@types/node": "^20.2.5",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"eslint": "^8.42.0",
"microbundle": "^0.15.1",
"prettier": "^2.8.7",
"prettier": "^2.8.8",
"prettier-plugin-organize-imports": "^3.2.2",
"typescript": "^5.0.2",
"vitest": "^0.29.7"
"typescript": "^5.1.3",
"vitest": "^0.32.0"
}
}

@@ -51,3 +51,3 @@ # ✓ valienv

_⚠️  In case of incorrect environment variables, the function will throw an `EnvValidationError` exposing `invalidVariables` and `missingVariables` names (not their values) to prevent your application from starting._
_⚠️  In case of incorrect environment variables, the function will throw an `EnvValidationError` exposing `variables` names (not their values) to prevent your application from starting._

@@ -93,7 +93,9 @@ #### overrides

// returns the result in case of valid value:
const port: Validator<number> = (value /*: string*/) => {
const parsed = parseInt(value);
const port: Validator<number> = (
value: string | undefined = "",
): number | undefined => {
const number = Number.parseInt(value);
if (parsed > 0 && parsed < 65536) {
return parsed;
if (number > 0 && number < 65536) {
return number;
}

@@ -131,3 +133,3 @@ };

// inlined validators return types are correctly inferred
ETHEREUM_ADDRESS: (value) => {
ETHEREUM_ADDRESS: (value = "") => {
if (validator.isEthereumAddress(value)) {

@@ -137,3 +139,3 @@ return value;

},
OPENED_COUNTRIES: (value) => {
OPENED_COUNTRIES: (value = "") => {
const array = value.split(",");

@@ -156,20 +158,7 @@

As it's a common pattern to have some optional environment values, you can write a small helper to wrap every validator with:
As it's a common pattern to have some optional environment values, we provide `optional`, a small helper to wrap every validator with:
```ts
import { string, validate } from "valienv";
import { optional, string, validate } from "valienv";
// Here's we are using a simple TS discriminating union:
type OptionalEnvValue<T> = { isSet: true; value: T } | { isSet: false };
const optional =
<T>(validator: Validator<T>): Validator<OptionalEnvValue<T>> =>
(value) => {
const result = validator(value);
return typeof result !== "undefined"
? { isSet: true, value: result }
: { isSet: false };
};
const env = validate({

@@ -182,8 +171,8 @@ env: process.env,

if (env.FOO.isSet) {
console.log(env.FOO.value); // FOO.value can only be accessed when isSet is true
if (env.FOO.defined) {
console.log(env.FOO.value); // FOO.value can only be accessed when defined is true
}
```
But you can also wrap them using a library of your choice. Here's an example with [`@swan-io/boxed`](https://github.com/swan-io/boxed):
You can also wrap validators using a library of your choice. Here's an example with [`@swan-io/boxed`](https://github.com/swan-io/boxed):

@@ -190,0 +179,0 @@ ```ts

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