Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
ADAPT1-1206 | Optimized V2 Contact Validation Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aadit-chugh committed Jun 7, 2024
1 parent 295d528 commit 002e096
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hydra.common.validation

import enumeratum.{Enum, EnumEntry}
import vulcan.AvroNamespace

import scala.collection.immutable

Expand Down Expand Up @@ -32,19 +31,13 @@ object AdditionalValidation {

lazy val allValidations: Option[List[AdditionalValidation]] =
Some(MetadataAdditionalValidation.values.toList ++ SchemaAdditionalValidation.values.toList)

lazy val allMetadataValidations: Option[List[AdditionalValidation]] =
Some(MetadataAdditionalValidation.values.toList)
}

class AdditionalValidationUtil(isExistingTopic: Boolean, currentAdditionalValidations: Option[List[AdditionalValidation]]) {

def pickValidations(): Option[List[AdditionalValidation]] =
if (isExistingTopic) currentAdditionalValidations else AdditionalValidation.allValidations

def pickMetadataValidations(): Option[List[AdditionalValidation]] =
if (isExistingTopic) currentAdditionalValidations else AdditionalValidation.allMetadataValidations

def isPresent(additionalValidation: AdditionalValidation): Boolean =
pickValidations().exists(_.contains(additionalValidation))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hydra.kafka.programs

import hydra.common.validation.ValidationError
import hydra.kafka.model.SubDataClassification
import hydra.kafka.model.TopicMetadataV2Request.Subject

sealed trait TopicMetadataError extends ValidationError

Expand Down Expand Up @@ -35,6 +34,6 @@ object TopicMetadataError {
}

case class InvalidContactProvided(contactField: String) extends TopicMetadataError {
override def message: String = s"Field `contact` must be a Slack channel starting with '#', all lowercase, with no spaces, and less than 80 characters, received '$contactField'!"
override def message: String = s"Field `slackChannel` must start with '#' and be entirely lowercase, without spaces, and less than 80 characters, received '$contactField'!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package hydra.kafka.programs

import cats.effect.Sync
import cats.syntax.all._
import hydra.common.validation.{AdditionalValidation, AdditionalValidationUtil, MetadataAdditionalValidation, Validator}
import hydra.common.validation.Validator.valid
import hydra.common.validation.{AdditionalValidation, AdditionalValidationUtil, MetadataAdditionalValidation, Validator}
import hydra.kafka.algebras.{KafkaAdminAlgebra, MetadataAlgebra}
import hydra.kafka.model.ContactMethod.Slack
import hydra.kafka.model.DataClassification._
import hydra.kafka.model.TopicMetadataV2Request.Subject
import hydra.kafka.model._
import hydra.kafka.services.{Invalid, Valid}
import hydra.kafka.util.MetadataUtils

class TopicMetadataV2Validator[F[_] : Sync](metadataAlgebra: MetadataAlgebra[F], kafkaAdmin: KafkaAdminAlgebra[F]) extends Validator {

Expand Down Expand Up @@ -72,26 +70,22 @@ class TopicMetadataV2Validator[F[_] : Sync](metadataAlgebra: MetadataAlgebra[F],
}

private def validateAdditional(additionalValidations: List[AdditionalValidation], request: TopicMetadataV2Request): F[Unit] = {
val maybeSlackChannel = extractSlackChannel(request)
val validations = additionalValidations.collect {
// Add extra validations applicable on topics created after replacementTopics feature was introduced.
case MetadataAdditionalValidation.replacementTopics => valid
case MetadataAdditionalValidation.contactValidation if request.contact.exists(_.isInstanceOf[Slack]) =>
val slackChannelValidation = extractSlackChannel(request).matches("""^#[a-z][a-z_-]{0,78}$""")
validate(slackChannelValidation, TopicMetadataError.InvalidContactProvided(extractSlackChannel(request)))
// val slackChannelValidation = extractSlackChannel(request).matches("""^#[a-z][a-z_-]{0,78}$""")
//// val slackChannelValidation = request.contact.exists {
//// case Slack(channel) => channel.value.matches("""^#[a-z][a-z_-]{0,78}$""")
//// case _ => false
//// }
// validate(slackChannelValidation, TopicMetadataError.InvalidContactProvided(extractSlackChannel(request)))
case MetadataAdditionalValidation.contactValidation if maybeSlackChannel.isDefined =>
val slackChannel = maybeSlackChannel.get
val slackChannelValidation = slackChannel.matches("""^#[a-z][a-z_-]{0,78}$""")
validate(slackChannelValidation, TopicMetadataError.InvalidContactProvided(slackChannel))
}
resultOf(validations.pure)
}

private def extractSlackChannel(request: TopicMetadataV2Request): String = {
private def extractSlackChannel(request: TopicMetadataV2Request): Option[String] = {
request.contact.collect {
case Slack(channel) => channel.value
}.headOption.getOrElse("Unknown Slack channel")
}.headOption
}

private def validateDeprecatedTopicHasReplacementTopic(deprecated: Boolean, replacementTopics: Option[List[String]], topic: String): F[Unit] = {
Expand Down

0 comments on commit 002e096

Please sign in to comment.