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

Dynamic default values #237

Open
ncjones opened this issue Jan 22, 2018 · 5 comments
Open

Dynamic default values #237

ncjones opened this issue Jan 22, 2018 · 5 comments

Comments

@ncjones
Copy link

ncjones commented Jan 22, 2018

I'd like to be able to define config defaults in terms of other configs. For example, the schema could allow a variable expansion syntax like the following:

  const schema = { 
    env: {
      format: '*',
      default: 'test'
    },
    baseUrl: { 
      format: 'url',
      default: 'http://${env}.my-domain/'
    },
  }

In the above case the call to config.get('baseUrl'), with no other configs defined, would return 'http://test.my-domain/'.

@bugeats
Copy link

bugeats commented Aug 23, 2018

Would this be better implemented with a hook function instead of string interpolation?

EDIT: there's nothing stopping you from using native JS variables and string interp.

@spikying
Copy link

spikying commented Apr 6, 2019

I would also like to use some computed default values, based on real values of other settings. I have put some workaround, but it would be nice to have the capacity to put a function instead of a static value in the defaults.

@sohailalam2
Copy link

Hi guys... any update on this? @ncjones I also have the same requirements, how did manage to get around yours?

@ncjones
Copy link
Author

ncjones commented Apr 9, 2019

I only use my environment name for dynamic defaults so I just have a special environment variable for this:

// src/config/schema.js
const env = process.env['ENV'] || 'local'
module.exports = {
  gcpProject: {
    format: String,
    default: `my-app-${env}`,
    env: 'GCP_PROJECT',
  }
}

@A-312
Copy link
Contributor

A-312 commented Dec 7, 2019

Actually you can with : #110 but :

since code too complicated already and only works in limited cases

In my opinion, the change should be done only after convict thing, not in live during the loading. This will add too many loop, we should check for each env change.

It is very simple to make a recursive function at the end. But if you really don't want to do with your own function. PR an compile function (#110 (comment)) :

convict.addCompiler('value', (v) => { /* ... */ })
config.load({
  gcpProject: {
    format: String,
    default: `my-app-${env}`,
    env: 'GCP_PROJECT',
  }
})
config.compile()
config.validate()

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

5 participants