Skip to main content

ObjectSource

The ObjectSource implementation of ConfigurationSource is quite straightforward; it simply uses a JavaScript Object as a source of configuration data. This is most useful for setting application defaults.

The default Configurator source list uses two ObjectSource implementations in the loading sequence; one is a low-priority source (set via a context field named "defaults") that can be used for application defaults (taking precedence over the schema defaults). A second ObjectSource is at the highest priority, and looks for values in the context under a field named "overrides".

All built-in sources including ObjectSource provide a constructor option allowing the developer to override the contextFieldName. This is used to create two instances that read from different fields in the context, as can be seen in the ConfigurationSource summary.

const configurator = new Configurator({schema});
configurator.registerConfigurationSource(new ObjectSource({contextFieldName: 'magic'}));

const config = await configurator.configure({
magic: { server: { hostname: 'localhost' } }
});
caution

This library unfortunately heavily overloads the word defaults.
Keep in mind the distinction between field defaults (loaded by DefaultsSource), and app defaults (loaded by ObjectSource).