node-convict icon indicating copy to clipboard operation
node-convict copied to clipboard

Cannot use dot in keys when using Object format

Open jonaskello opened this issue 6 years ago • 5 comments

I have a case where an URL is a key in an object that is a default value for a setting. Something like this:

export const conf = convict({
  npmrc: {
    doc: "Config for installing plugin packages.",
    format: "Object",
    default: {
      registries: {
        "@foo": "https://npm.foo.com/",
      },
      authTokens: {
        "https://npm.foo.com/": {
          type: "Bearer",
          token: "XXXX",
        },
      },
    },
  }
}

This fails to validate with this error:

Error: cannot find configuration param 'npmrc.authTokens.https://npm.foo.com/'
    at walk (/XXXserver.js:74759:15)
    at flatten (/XXX/server.js:74405:15)
    at validate (/XXX/server.js:74449:24)
    at Object.validate (/XXX/server.js:74911:20)

I guess it is becuase convict supports dot notation in the string sent to get() which gets confusing when the keys also have dots. Is there a workaround for this so we can use URLs as keys?

jonaskello avatar May 09 '18 22:05 jonaskello

I'm having this problem with String type as well. It looks like this isn't specific to Objects.

noderat avatar May 21 '18 22:05 noderat

Sorry @jonaskello, no workaround that I know of.

The way convict parses all the objects is too complicated. This part is getting hard to maintain. The problem you are mentioning is a proof of it. A very good part of convict is the thorough tests which provide the needed cover and confidence when we modiy convict. A good thing would be to rewrite the whole parsing of objects. Taking advantage of ES6 features like the Map data structure, instead of using plain Objects, would be the way to go.

madarche avatar May 26 '18 08:05 madarche

This is hitting us pretty hard as the end-user is putting in config vals, not ourselves.

lmeyerov avatar Aug 28 '19 06:08 lmeyerov

Wrapping in [...] seems to protect against this, at least preventing crashing.

lmeyerov avatar Aug 31 '19 00:08 lmeyerov

#354 ;)

A-312 avatar Jan 06 '20 23:01 A-312