Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set default value for a JSON env variable #95

Closed
alfonmga opened this issue Feb 5, 2019 · 5 comments
Closed

Unable to set default value for a JSON env variable #95

alfonmga opened this issue Feb 5, 2019 · 5 comments

Comments

@alfonmga
Copy link

alfonmga commented Feb 5, 2019

I'm getting this error:

================================
 Invalid environment variables:
    POSTMARK_TEMPLATES_ID: Invalid json: "[object Object]"
    POSTMARK_TEMPLATE_VALUES: Invalid json: "[object Object]"

 Exiting with error code 1
================================

And here's my code:

// config/index.js

const envalid = require('envalid');

const { str, num, json } = envalid;

const env = envalid.cleanEnv(
  process.env,
  {
    APP_MODE: str({ default: 'dev' }),
    POSTMARK_TEMPLATES_ID: json({
      default: {
        VERIFY_EMAIL: 1,
        FORGOT_PASSWORD: 2,
        PASSWORD_RESETED: 3,
        PASSWORD_CHANGED: 4,
        EMAIL_CHANGED: 5,
      },
    }),
    POSTMARK_TEMPLATE_VALUES: json({
      default: {
        product_name: 'ACME Software v1.0',
        product_url: 'https://domain.io',
        support_url: 'https://support.domain.io',
        company_name: 'ACME Inc.',
        company_address: null,
      },
    }),
  },
  {
    dotEnvPath: '.env',
  },
);

// eslint-disable-next-line import/no-dynamic-require
const modeConfig = require(`./${env.APP_MODE}`);

module.exports = Object.freeze({
  ...env,
  ...modeConfig,
});

I'm not sure at all if I'm doing something wrong, but I believe it is a bug.

@af
Copy link
Owner

af commented Feb 5, 2019

Try a JSON string for your default values (not an object). I think that should fix the error.

For example:

default: `{
  "product_name": "ACME Software v1.0",
  ...(rest of json string here)
}`

@alfonmga
Copy link
Author

alfonmga commented Feb 5, 2019

@af unfortunately, it does not work. I'm getting the same error message: Invalid json.

@SimenB
Copy link
Collaborator

SimenB commented Feb 6, 2019

Do JSON.stringify around your object

@alfonmga
Copy link
Author

alfonmga commented Feb 6, 2019

@SimenB it worked, thanks!

P.S. I think maybe we should add an example to the README.md.

@alfonmga alfonmga closed this as completed Feb 6, 2019
@SimenB
Copy link
Collaborator

SimenB commented Feb 6, 2019

Sure, PR welcome 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants