codemariner/tconf

Unable to load optional values from env

Loskir opened this issue · 4 comments

Steps to reproduce:

  1. Add an optional value to the schema. {test: Optional(String)}
  2. Map it to some env variable in yaml. test: ${TEST_ENV}
  3. Set the env, run the file and see that it's undefined.
DEBUG=tconf* TEST_ENV=123 node index.js                                                                                                                                                                                                         
  tconf loading files from ./config/: default, NODE_ENV, ENV, local +0ms
  tconf:file parsing config config/default.yaml +0ms
  tconf:env unable to coerce value from env var "test" as type optional +0ms
  tconf:file config file config/NODE_ENV.yaml not found +2ms
  tconf inspecting environment variables +3ms
  tconf:file config file config/local.yaml not found +0ms
{ test: undefined }

Example: https://github.com/Loskir/tconf-test

Mapped variable also doesn't work

DEBUG=tconf* CONFIG_test=123 node index.js                                                                                                                                                                                                      
  tconf loading files from ./config/: default, NODE_ENV, ENV, local +0ms
  tconf:file parsing config config/default.yaml +0ms
  tconf:file config file config/NODE_ENV.yaml not found +1ms
  tconf inspecting environment variables +3ms
  tconf:env processing env var "CONFIG_test" +0ms
  tconf:env retrieving type information from test +0ms
  tconf:env coercing value to optional +0ms
  tconf:env unable to coerce value from env var "CONFIG_test" as type optional +0ms
  tconf:file config file config/local.yaml not found +1ms
{}

Hi @Loskir. Yeah, looks like that's a gap. For now, you can use Partial instead of inline Optional values. For example:

const ConfigSpec = Partial({
   test: Number
});

if you need to combine with required fields, you can use And like here.

Hi @Loskir. This has been fixed in release 2.2.2.. You should be able to map environment variables to Optional types now. Please let me know if you have run into any more problems.

Thank you!