Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation to LEI in data browser #4826

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/src/main/scala/hmda/messages/HmdaMessageFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hmda.messages

import akka.kafka.ConsumerMessage.{CommittableMessage, CommittableOffset}
import com.typesafe.scalalogging.StrictLogging
import hmda.util.LEIValidator

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
Expand All @@ -16,10 +17,9 @@ object HmdaMessageFilter extends StrictLogging {

def parse(key: String, value: String): Option[StandardMsg] = {
Try {
val leiRegex = "(?<lei>[A-Z0-9]+)"
val keyRegex = s"^${leiRegex}$$".r
val keyRegex = s"^${LEIValidator.leiKeyRegex}$$".r
// lei1:lei2-year-q1-seq_num
val msgRegex = s"^${leiRegex}-(?<year>[0-9]{4})(-(?<quarter>[qQ][1-3]))?(-(?<seqNum>[0-9]+))?$$".r
val msgRegex = s"^${LEIValidator.leiKeyRegex}-(?<year>[0-9]{4})(-(?<quarter>[qQ][1-3]))?(-(?<seqNum>[0-9]+))?$$".r
for {
keyMatch <- keyRegex.findFirstMatchIn(key)
msgMatch <- msgRegex.findFirstMatchIn(value)
Expand Down
12 changes: 12 additions & 0 deletions common/src/main/scala/hmda/util/LEIValidator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hmda.util

object LEIValidator {

val leiRegex = "([A-Z0-9]{20})"

val leiKeyRegex = "(?<lei>[A-Z0-9]{20})"

def isValidLEIFormat(lei: String): Boolean =
lei.matches(leiRegex)

}
12 changes: 6 additions & 6 deletions common/src/test/scala/hmda/message/HmdaMessageFilterTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import org.scalatest.FunSuite
class HmdaMessageFilterTest extends FunSuite {

testParse(
"LEI",
"LEI-2020",
Some(StandardMsg("LEI", 2020, None, None))
"B90YWS6AFX2LGWOXJ1LD",
"B90YWS6AFX2LGWOXJ1LD-2020",
Some(StandardMsg("B90YWS6AFX2LGWOXJ1LD", 2020, None, None))
)

testParse(
"LEI",
"LEI-2020-q1",
Some(StandardMsg("LEI", 2020, Some("q1"), None))
"B90YWS6AFX2LGWOXJ1LD",
"B90YWS6AFX2LGWOXJ1LD-2020-q1",
Some(StandardMsg("B90YWS6AFX2LGWOXJ1LD", 2020, Some("q1"), None))
)

testParse(
Expand Down
16 changes: 16 additions & 0 deletions common/src/test/scala/hmda/util/LEIValidatorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hmda.util

import hmda.util.LEIValidator._
import org.scalatest.{MustMatchers, PropSpec}
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class LEIValidatorSpec extends PropSpec with ScalaCheckPropertyChecks with MustMatchers {

property("correctly validates LEIs") {
isValidLEIFormat("ABCHD73849OKDJFIRB63") mustBe true
isValidLEIFormat("ABCHD73849HDJUSKAB6") mustBe false
isValidLEIFormat("ABCHD73849JFUDHSNB 6") mustBe false
isValidLEIFormat("ABCHD73849HFUDJENB-6") mustBe false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import hmda.dataBrowser.models.State._
import hmda.dataBrowser.models.County._
import hmda.dataBrowser.models.TotalUnits._
import hmda.dataBrowser.models._
import hmda.util.LEIValidator._
import Delimiter.fileEnding
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import hmda.dataBrowser.services._
Expand Down Expand Up @@ -155,6 +156,9 @@ trait DataBrowserDirectives extends Settings {
private def extractLEIs: Directive1[Option[QueryField]] =
parameters("leis".as(CsvSeq[String]) ? Nil).flatMap {
case Nil => provide(None)
case xs if xs.exists(lei => !isValidLEIFormat(lei)) =>
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
complete((BadRequest, s"A Valid LEI should be 20 characters and Alphanumeric (${xs.mkString(", ")})"))
case xs =>
provide(Option(QueryField(name = "lei", xs.map(_.toString), dbName = "lei", isAllSelected = false)))
}
Expand Down