Proper way to parse environment variables
Asked Answered
B

3

9

I am using node-config in basically all my projects and most of the time I come across the problem of parsing booleans and numbers which are set as environment variables.

E.g.

default.js

module.exports = { 
    myNumber = 10,
    myBool = true
}

custom-environment-variables.js

module.exports = { 
    myNumber = "MY_NUMBER",
    myBool = "MY_BOOL"
}

Now, the obvious problem is that if I override the default values with custom values set as environment variables they will be a string value instead of a number or boolean value. So now, to make sure in my code that the types are correct. I always have to do type conversion and for booleans use a proper library e.g. yn. The problem is I have to do this conversion every time I use config.get() for example +config.get("myNumber") or yn(config.get("myBool")).

Is there a better and more elegant way to do this?

One solution I see would be to add a type property to an environment variable as it is done here with format. This would allow to do something like this...

custom-environment-variables.js

module.exports = { 
    myNumber = {
        name: "MY_NUMBER",
        type: "number"
    },
    myBool = {
        name: "MY_BOOL",
        type: "boolean"
    }
}

node-config would handle the type conversions and there would be no need to do it all the time in the code when getting it. Of course there would be the requirement to implement a proper parser for booleans but those already exist and could be used here.

Bagehot answered 5/1, 2020 at 11:16 Comment(0)
B
1

This feature is now supported in node-config v3.3.2, see changelog

Bagehot answered 27/9, 2020 at 17:53 Comment(0)
V
6

By default, environment variables will be parsed as string. In node-config, we could override this behaviour with __format as shown below.

We don't need any additional libraries. Normal json datatypes like boolean, number, nested json etc., should work well.

Taking an easy to relate example.

config/default.json

{
  "service": {
    "autostart": false
  }
}

custom-environment-variables.json

{
  "service": {
    "autostart": {
      "__name": "AUTOSTART",
      "__format": "json"
    }
  }
}

Now we can pass environment variables when we like to override and no type conversation should be needed for basic types.

Vestry answered 9/6, 2020 at 11:52 Comment(1)
there is an open PR on GitHub related to the issue to make the __format a bit more specific in case of numbers and booleans but in general your answer would work as wellBagehot
B
1

This feature is now supported in node-config v3.3.2, see changelog

Bagehot answered 27/9, 2020 at 17:53 Comment(0)
D
0

I use this method:

const toBoolean = (dataStr) => {
  return !!(dataStr?.toLowerCase?.() === 'true' || dataStr === true);
};

You can add cases if you want 0 to resolve to true as well:

const toBoolean = (dataStr) => {
  return !!(dataStr?.toLowerCase?.() === 'true' || dataStr === true || Number.parseInt(dataStr, 10) === 0);
};
Dijon answered 6/8, 2022 at 17:24 Comment(1)
Logically 1 = true and 0 = falseQuintinquintina

© 2022 - 2024 — McMap. All rights reserved.