Skip to content

Commit

Permalink
Extract validation to function and reduce branching
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelManke committed Sep 3, 2022
1 parent 2f28879 commit 01b4f41
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/@aws-cdk/aws-lambda/lib/event-source-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,16 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
selfManagedEventSource = { endpoints: { kafkaBootstrapServers: props.kafkaBootstrapServers } };
}

let amazonManagedKafkaEventSourceConfig : CfnEventSourceMapping['amazonManagedKafkaEventSourceConfig'];
let selfManagedKafkaEventSourceConfig: CfnEventSourceMapping['selfManagedKafkaEventSourceConfig'];

if (props.kafkaConsumerGroupId) {
if (props.kafkaConsumerGroupId.length > 200 || props.kafkaConsumerGroupId.length <1) {
throw new Error('kafkaConsumerGroupId must be a valid string between 1 and 200 characters');
let amazonManagedKafkaEventSourceConfig: CfnEventSourceMapping['amazonManagedKafkaEventSourceConfig'];
if (props.kafkaConsumerGroupId && props.eventSourceArn) {
this.validateKafkaConsumerGroupIdOrThrow(props.kafkaConsumerGroupId);
amazonManagedKafkaEventSourceConfig = { consumerGroupId: props.kafkaConsumerGroupId };
}

const regex = new RegExp(/[a-zA-Z0-9-\/*:_+=.@-]*/);
const patternMatch = regex.exec(props.kafkaConsumerGroupId);
if (patternMatch === null || patternMatch[0] !== props.kafkaConsumerGroupId) {
throw new Error('kafkaConsumerGroupId contain ivalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
}

if (Boolean(props.kafkaBootstrapServers?.length) ) {
let selfManagedKafkaEventSourceConfig: CfnEventSourceMapping['selfManagedKafkaEventSourceConfig'];
if (props.kafkaConsumerGroupId && props.kafkaBootstrapServers) {
this.validateKafkaConsumerGroupIdOrThrow(props.kafkaConsumerGroupId);
selfManagedKafkaEventSourceConfig = { consumerGroupId: props.kafkaConsumerGroupId };
} else {
amazonManagedKafkaEventSourceConfig = { consumerGroupId: props.kafkaConsumerGroupId };
}
}

const cfnEventSourceMapping = new CfnEventSourceMapping(this, 'Resource', {
Expand All @@ -386,6 +377,18 @@ export class EventSourceMapping extends cdk.Resource implements IEventSourceMapp
});
this.eventSourceMappingId = cfnEventSourceMapping.ref;
}

private validateKafkaConsumerGroupIdOrThrow(kafkaConsumerGroupId: string) {
if (kafkaConsumerGroupId.length > 200 ||kafkaConsumerGroupId.length < 1) {
throw new Error('kafkaConsumerGroupId must be a valid string between 1 and 200 characters');
}

const regex = new RegExp(/[a-zA-Z0-9-\/*:_+=.@-]*/);
const patternMatch = regex.exec(kafkaConsumerGroupId);
if (patternMatch === null || patternMatch[0] !== kafkaConsumerGroupId) {
throw new Error('kafkaConsumerGroupId contain ivalid characters. Allowed values are "[a-zA-Z0-9-\/*:_+=.@-]"');
}
}
}

/**
Expand Down

0 comments on commit 01b4f41

Please sign in to comment.