Skip to content

Commit

Permalink
allows plugins to define validation schema for "enabled" flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Nov 12, 2019
1 parent 57f43cc commit 910c9d9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
69 changes: 69 additions & 0 deletions src/core/server/config/config_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,33 @@ test('handles disabled path and marks config as used', async () => {
expect(unusedPaths).toEqual([]);
});

test('does not throw if schema does not define "enabled" schema', async () => {
const initialConfig = {
pid: {
file: '/some/file.pid',
},
};

const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);
expect(
configService.setSchema(
'pid',
schema.object({
file: schema.string(),
})
)
).resolves.toBeUndefined();

const value$ = configService.atPath('pid');
const value: any = await value$.pipe(first()).toPromise();
expect(value.enabled).toBe(undefined);

const valueOptional$ = configService.optionalAtPath('pid');
const valueOptional: any = await valueOptional$.pipe(first()).toPromise();
expect(valueOptional.enabled).toBe(undefined);
});

test('treats config as enabled if config path is not present in config', async () => {
const initialConfig = {};

Expand All @@ -292,3 +319,45 @@ test('treats config as enabled if config path is not present in config', async (
const unusedPaths = await configService.getUnusedPaths();
expect(unusedPaths).toEqual([]);
});

test('read "enabled" even if its schema is not present', async () => {
const initialConfig = {
foo: {
enabled: true,
},
};

const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);

const isEnabled = await configService.isEnabledAtPath('foo');
expect(isEnabled).toBe(true);
});

test('allows plugins to specify "enabled" flag via validation schema', async () => {
const initialConfig = {};

const config$ = new BehaviorSubject(new ObjectToConfigAdapter(initialConfig));
const configService = new ConfigService(config$, defaultEnv, logger);

await configService.setSchema(
'foo',
schema.object({ enabled: schema.boolean({ defaultValue: false }) })
);

expect(await configService.isEnabledAtPath('foo')).toBe(false);

await configService.setSchema(
'bar',
schema.object({ enabled: schema.boolean({ defaultValue: true }) })
);

expect(await configService.isEnabledAtPath('bar')).toBe(true);

await configService.setSchema(
'baz',
schema.object({ different: schema.boolean({ defaultValue: true }) })
);

expect(await configService.isEnabledAtPath('baz')).toBe(true);
});
20 changes: 17 additions & 3 deletions src/core/server/config/config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,28 @@ export class ConfigService {
}

public async isEnabledAtPath(path: ConfigPath) {
const enabledPath = createPluginEnabledPath(path);
const namespace = pathToString(path);

const validatedConfig = this.schemas.has(namespace)
? await this.atPath<{ enabled?: boolean }>(path)
.pipe(first())
.toPromise()
: undefined;

const enabledPath = createPluginEnabledPath(path);
const config = await this.config$.pipe(first()).toPromise();
if (!config.has(enabledPath)) {

// if plugin hasn't got a config schema, we try to read "enabled" directly
const isEnabled =
validatedConfig && validatedConfig.enabled !== undefined
? validatedConfig.enabled
: config.get(enabledPath);

// not declared. consider that plugin is enabled by default
if (isEnabled === undefined) {
return true;
}

const isEnabled = config.get(enabledPath);
if (isEnabled === false) {
// If the plugin is _not_ enabled, we mark the entire plugin path as
// handled, as it's expected that it won't be used.
Expand Down

0 comments on commit 910c9d9

Please sign in to comment.