mozilla/node-convict

How to use a config param with format Object in convict node module

Closed this issue · 7 comments

I have a "publicKey" attribute in my config which is an object of some defined interface type.
I'm defining this "publicKey" param in convict schema as:

  publicKey: {
    doc: 'xxxxxxxxxxx',
    format: Object,
    default: undefinedDefault as JWK.RawKey,
  },

where undefinedDefault is: const undefinedDefault = undefined as unknown;

When in config, I provide this public key object, then schema validation fails with this error:

/opt/dev/node_modules/convict/lib/convict.js:686
          throw new Error(output);
                ^
Error: idps: configuration param 'publicKey' missing from config, did you override its parent?: value was [{"publicKey":{"kty":"xxx","kid":"xxx"}]
    at Object.validate (/opt/dev/node_modules/convict/lib/convict.js:686:17)
    at ApplicationConfig.loadFile (/opt/dev/ts/packages/base-config/src/app.ts:56:49)
    at Function.getInstance (/opt/dev/ts/packages/base-config/src/app.ts:210:22)
    at Object.exports.getSingletonConfig (/opt/dev/ts/microservices/solution-registry/src/config.ts:239:28)
    at new AuthorizerModel (/opt/dev/ts/microservices/solution-registry/src/models/authorizer-model.ts:21:20)
    at Object.<anonymous> (/opt/dev/ts/microservices/solution-registry/src/models/authorizer-model.ts:46:25)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1059:12)

So how to use this Object format in convict schema and define a default for such attributes?

value was [{"publicKey":{"kty":"xxx","kid":"xxx"}]

Besides the fact that that's not even a valid JSON (which is impossible), the object should probably not be wrapped inside an Array.

value was [{"publicKey":{"kty":"xxx","kid":"xxx"}]

Besides the fact that that's not even a valid JSON (which is impossible), the object should probably not be wrapped inside an Array.

that is actually a trimmed response, the config file has other entries too and this public key is an entry for one key (which is an array), I have removed those.

Alright, but more details about your config would be nice, especially how you the declared the parents of publicKey. Your error description (configuration param 'publicKey') implies there are none, but there are, right?

In that case: You cannot describe complex array types. If you want to, you would have to create your own format, that checks that the specified value is an array and matches the structure that you expect. Hope that helps.

In that case: You cannot describe complex array types. If you want to, you would have to create your own format, that checks that the specified value is an array and matches the structure that you expect. Hope that helps.

Yeah tried this format option (custom validation check) and this works, thanks!

Thanks for sharing the insights, @harshitachouhan206. (I read the original comment)

So the real issue is that convict doesn't expect undefined as default value when format is set to Object.
Your use case basically relates to #292, which gets solved using a nullable option that's ready to be released.

For now it's probably best to just specify your own custom format for publicKey, which allows null or undefined additionally to an Object.

So the real issue is that convict doesn't expect undefined as default value when format is set to Object.

Yeah, correct.
And that 'nullable' option would be helpful. For now, I'm going with the custom format checking.
Thanks for your help and for more info.

Closing this issue, as it is resolved now.