Giter Site home page Giter Site logo

Comments (3)

SimenB avatar SimenB commented on July 23, 2024

I think I'd merge the specifications (that is, export SHARED_THING: envalid.str(... instead of passing it to cleanEnv) then do a cleanEnv from your entrypoint.

Thoughts?

from envalid.

kachkaev avatar kachkaev commented on July 23, 2024

I had that in mind as well, so that solution would be something like:

// config.ts
import * as envalid from "envalid";

export const sharedValidators = {
  SHARED_THING: envalid.str({
    default: "this variable needs to present in all scripts",
  }),
};
// peculiarTask.ts
import * as envalid from "envalid";
import { sharedValidators } from "./config";

const env = envalid.cleanEnv(process.env, {
  ...sharedValidators,
  EXTRA_THING: envalid.str({
    default: "this variable should only be cleaned inside peculiarTask.ts",
  }),
});

This looks rather clean so far, but has two issues:

  1. I have to import envalid from "envalid" in every entrypoint and run const env = envalid.cleanEnv(process.env, sharedValidators, { strict: true }) despite that most of my commands have no peculiar configs.

  2. I can't derive stuff from the shared env in my config.ts such as:

// config.ts
import * as envalid from "envalid";
import SharedThingManager from "things";
import * as path from "path";

export const env = envalid.cleanEnv(process.env, {
  SHARED_THING: envalid.str({
    default: "this variable needs to present in all scripts",
  }),
}, { strict: true });

// example 1
export const sharedThingManager = new SharedThingManager(env.SHARED_THING);

// example 2
export const resolvedSharedThing = path.resolve(__dirname, env.SHARED_THING);

from envalid.

af avatar af commented on July 23, 2024

This has been open for a long while and I think the solution of sharing the config (like below) is the best way to go. If there's a more actionable proposal, please open a new issue, going to close this one now

const env = envalid.cleanEnv(process.env, {
  ...sharedValidators,
  EXTRA_THING: envalid.str({
    default: "this variable should only be cleaned inside peculiarTask.ts",
  }),
});

from envalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.