Skip to content

Commit

Permalink
Merge pull request #4369 from kgudel/checkDateFormatInParser
Browse files Browse the repository at this point in the history
Add More Robust Parser Check for Date
  • Loading branch information
PatrickGoRaft authored Feb 8, 2022
2 parents 5ce1bea + 8fa964a commit 5026b89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common/src/main/resources/parserFieldValidValues.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
LEI|String with only letters and numbers
ULI|String with only letters and numbers
LAR Record Identifier|Integer: 2
Application Date|Integer or NA
Application Date|Valid Date in format YYYYMMDD or NA
Loan Type|Integer: 1, 2, 3, 4
Loan Purpose|Integer: 1, 2, 31, 32, 4, 5
Preapproval|Integer: 1, 2
Construction Method|Integer: 1, 2
Occupancy Type|Integer: 1, 2, 3
Action Taken|Integer: 1, 2, 3, 4, 5, 6, 7, 8
Action Taken Date|Integer
Action Taken Date|Valid Date in format YYYYMMDD
Loan Amount|Double
Loan Term|Integer, NA, or Exempt
Ethnicity of Applicant or Borrower: 1|Integer: 1, 11, 12, 13, 14, 2, 3, 4, 5 or Blank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ sealed trait LarFormatValidator extends LarParser {
): LarParserValidationResult[Loan] =
(
validateStrNoSpace(uli, InvalidULI(uli)),
validateIntStrOrNAField(applicationDate, InvalidApplicationDate(applicationDate)),
validateDateOrNaField(applicationDate, InvalidApplicationDate(applicationDate)),
validateLarCode(LoanTypeEnum, loanType, InvalidLoanType(loanType)),
validateLarCode(LoanPurposeEnum, loanPurpose, InvalidLoanPurpose(loanPurpose)),
validateLarCode(ConstructionMethodEnum, constructionMethod, InvalidConstructionMethod(constructionMethod)),
Expand All @@ -505,7 +505,7 @@ sealed trait LarFormatValidator extends LarParser {
(
validateLarCode(PreapprovalEnum, preapproval, InvalidPreapproval(preapproval)),
validateLarCode(ActionTakenTypeEnum, actionTaken, InvalidActionTaken(actionTaken)),
validateIntField(actionDate, InvalidActionTakenDate(actionDate))
validateDateField(actionDate, InvalidActionTakenDate(actionDate))
).mapN(LarAction.apply)

def validateGeography(
Expand Down
16 changes: 16 additions & 0 deletions common/src/main/scala/hmda/parser/filing/lar/LarParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.implicits._
import hmda.model.filing.lar.enums.LarCodeEnum
import hmda.parser.LarParserValidationResult
import hmda.parser.ParserErrorModel.ParserValidationError
import java.time.format.DateTimeFormatter

import scala.util.{ Failure, Success, Try }

Expand Down Expand Up @@ -109,6 +110,21 @@ trait LarParser {
str.validNel
else
parserValidationError.invalidNel

def validateDateField(str: String, parserValidationError: ParserValidationError): LarParserValidationResult[Int] = {
val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
Try(dateFormatter.parse(str)) match {
case Success(i) => str.toInt.validNel
case Failure(_) => parserValidationError.invalidNel
}
}

def validateDateOrNaField(str: String, parserValidationError: ParserValidationError): LarParserValidationResult[String] =
if (str == "NA") {
str.validNel
} else {
validateDateField(str, parserValidationError).map(x => x.toString)
}

def validateLarCode[A](larCodeEnum: LarCodeEnum[A],
value: String,
Expand Down

0 comments on commit 5026b89

Please sign in to comment.