Skip to content

Commit

Permalink
refactor: move runner config validation outside constructor
Browse files Browse the repository at this point in the history
This allows lazy validation of the configuration. This is necessary for merging the runner configuration with the docker-compose configuration before validation.
  • Loading branch information
n1ru4l committed Sep 23, 2019
1 parent 593b582 commit 98d0e60
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Dockest {
// Validate service name uniqueness
const serviceMap: ObjStrStr = {}
for (const runner of this.config.runners) {
runner.validateConfig()
if (serviceMap[runner.runnerConfig.service]) {
throw new ConfigurationError(
`Service property has to be unique. Collision found for runner with service "${runner.runnerConfig.service}"`,
Expand Down
1 change: 1 addition & 0 deletions src/runners/@types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface BaseRunner {
initializer: string
runnerConfig: RunnerConfig
logger: Logger
validateConfig: () => void
}

export interface SharedRequiredConfigProps {
Expand Down
2 changes: 1 addition & 1 deletion src/runners/GeneralPurposeRunner/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('GeneralPurposeRunner', () => {

it('should fail validation', () => {
// @ts-ignore
expect(() => new GeneralPurposeRunner({})).toThrow(/service: Schema-key missing in config/)
expect(() => new GeneralPurposeRunner({}).validateConfig()).toThrow(/service: Schema-key missing in config/)
})
})
2 changes: 2 additions & 0 deletions src/runners/GeneralPurposeRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class GeneralPurposeRunner implements BaseRunner {
}

this.logger = new Logger(this)
}

public validateConfig() {
const schema: { [key in keyof RequiredConfigProps]: any } = {
service: validateTypes.isString,
}
Expand Down
2 changes: 1 addition & 1 deletion src/runners/KafkaRunner/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ describe('KafkaRunner', () => {

it('should fail validation', () => {
// @ts-ignore
expect(() => new KafkaRunner({})).toThrow(/service: Schema-key missing in config/)
expect(() => new KafkaRunner({}).validateConfig()).toThrow(/service: Schema-key missing in config/)
})
})
2 changes: 2 additions & 0 deletions src/runners/KafkaRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class KafkaRunner implements BaseRunner {
...config,
}
this.logger = new Logger(this)
}

public validateConfig() {
const schema: { [key in keyof RequiredConfigProps]: any } = {
service: validateTypes.isString,
}
Expand Down
2 changes: 1 addition & 1 deletion src/runners/PostgresRunner/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('PostgresRunner', () => {

it('should fail validation', () => {
// @ts-ignore
expect(() => new PostgresRunner({ database: '_', password: '_', username: '_' })).toThrow(
expect(() => new PostgresRunner({ database: '_', password: '_', username: '_' }).validateConfig()).toThrow(
/service: Schema-key missing in config/,
)
})
Expand Down
2 changes: 2 additions & 0 deletions src/runners/PostgresRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class PostgresRunner implements BaseRunner {
...configUserInput,
}
this.logger = new Logger(this)
}

public validateConfig() {
// TODO: Can this type be generalized and receive RequiredConfigProps as an argument?
const schema: { [key in keyof RequiredConfigProps]: () => void } = {
database: validateTypes.isString,
Expand Down
2 changes: 1 addition & 1 deletion src/runners/RedisRunner/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('RedisRunner', () => {

it('should fail validation', () => {
// @ts-ignore
expect(() => new RedisRunner({})).toThrow(/service: Schema-key missing in config/)
expect(() => new RedisRunner({}).validateConfig()).toThrow(/service: Schema-key missing in config/)
})
})
2 changes: 2 additions & 0 deletions src/runners/RedisRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class RedisRunner implements BaseRunner {
...config,
}
this.logger = new Logger(this)
}

public validateConfig() {
const schema: { [key in keyof RequiredConfigProps]: any } = {
service: validateTypes.isString,
}
Expand Down
2 changes: 1 addition & 1 deletion src/runners/ZooKeeperRunner/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('ZooKeeperRunner', () => {

it('should fail validation', () => {
// @ts-ignore
expect(() => new ZooKeeperRunner({})).toThrow(/service: Schema-key missing in config/)
expect(() => new ZooKeeperRunner({}).validateConfig()).toThrow(/service: Schema-key missing in config/)
})
})
2 changes: 2 additions & 0 deletions src/runners/ZooKeeperRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ZooKeeperRunner implements BaseRunner {
...config,
}
this.logger = new Logger(this)
}

public validateConfig() {
const schema: { [key in keyof RequiredConfigProps]: any } = {
service: validateTypes.isString,
}
Expand Down

0 comments on commit 98d0e60

Please sign in to comment.