Skip to content

Commit

Permalink
Use a slash ('/') instead of '-' to separate day, month and year so t…
Browse files Browse the repository at this point in the history
…hat it can validate a negative number if the user passes via HTML form
  • Loading branch information
vimleshtna committed Nov 13, 2023
1 parent f7c1427 commit 69688fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ case object DateTime extends DataType with Product with Serializable {
case "" if criteria.required => Some(EMPTY_VALUE_ERROR)
case "" if !criteria.required => None
case v =>
val date = v.replaceAll("[T ]", "-").split("[-:]")
val date = v.replaceAll("[T ]", ":").split("[/:]")
if (date.length < 6) {
Some(INVALID_DATE_FORMAT_ERROR)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,64 @@ class DataTypeSpec extends AnyWordSpec {
val criteria = MetadataCriteria("Property1", DateTime, required = true, isFutureDateAllowed = false, isMultiValueAllowed = false, Nil, None, None)

"checkValue should not return any errors if the date is valid" in {
DateTime.checkValue("1990-12-10T00:00:00", criteria) should be(None)
DateTime.checkValue("2000-2-29T00:00:00", criteria) should be(None)
DateTime.checkValue("1990-01-01 00:00:00.0", criteria) should be(None)
DateTime.checkValue("1990/12/10T00:00:00", criteria) should be(None)
DateTime.checkValue("2000/2/29T00:00:00", criteria) should be(None)
DateTime.checkValue("1990/01/01 00:00:00.0", criteria) should be(None)
DateTime.checkValue("1990:01:01T00:00:00.0", criteria) should be(None)
}

"checkValue should not return any errors if the value is empty but it is not mandatory" in {
DateTime.checkValue("", criteria.copy(required = false)) should be(None)
}

"checkValue should not return any errors if future date is allowed" in {
DateTime.checkValue("1990-12-10T00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
DateTime.checkValue("2090-12-10T00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
DateTime.checkValue("2090-12-10 00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
DateTime.checkValue("1990/12/10T00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
DateTime.checkValue("2090/12/10T00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
DateTime.checkValue("2090/12/10 00:00:00", criteria.copy(isFutureDateAllowed = true)) should be(None)
}

"checkValue should return an error if the date format is invalid" in {
DateTime.checkValue("1990/12/10T00:00:00", criteria) should be(Some(INVALID_DATE_FORMAT_ERROR))
DateTime.checkValue("2000-2-29", criteria) should be(Some(INVALID_DATE_FORMAT_ERROR))
DateTime.checkValue("1990-12-10T00:00:00", criteria) should be(Some(INVALID_DATE_FORMAT_ERROR))
DateTime.checkValue("2000/2/29", criteria) should be(Some(INVALID_DATE_FORMAT_ERROR))
}

"checkValue should return an error if the day/month/year in negative" in {
DateTime.checkValue("1990:12:-10T00:00:00", criteria) should be(Some(NEGATIVE_NUMBER_ERROR_FOR_DAY))
DateTime.checkValue("1990:-12:10T00:00:00", criteria) should be(Some(NEGATIVE_NUMBER_ERROR_FOR_MONTH))
DateTime.checkValue("-1990:12:10T00:00:00", criteria) should be(Some(NEGATIVE_NUMBER_ERROR_FOR_YEAR))
}

"checkValue should return an error if the value is empty and mandatory" in {
DateTime.checkValue("", criteria) should be(Some(EMPTY_VALUE_ERROR))
DateTime.checkValue("--T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR))
DateTime.checkValue("//T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR))
}

"checkValue should return an error if the day or month or year is empty" in {
DateTime.checkValue("1990-10-T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_DAY))
DateTime.checkValue("1990--10T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_MONTH))
DateTime.checkValue("-10-10T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_YEAR))
DateTime.checkValue("-10-10 00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_YEAR))
DateTime.checkValue("1990/10/T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_DAY))
DateTime.checkValue("1990//10T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_MONTH))
DateTime.checkValue("/10/10T00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_YEAR))
DateTime.checkValue("/10/10 00:00:00", criteria) should be(Some(EMPTY_VALUE_ERROR_FOR_YEAR))
}

"checkValue should return an error if the day or month or year is not valid" in {
DateTime.checkValue("1990-10-32T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_DAY))
DateTime.checkValue("1990-28-10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_MONTH))
DateTime.checkValue("19999-10-10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
DateTime.checkValue("199-10-10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
DateTime.checkValue("199-10-10 00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
DateTime.checkValue("1990/10/32T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_DAY))
DateTime.checkValue("1990/28/10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_MONTH))
DateTime.checkValue("19999/10/10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
DateTime.checkValue("199/10/10T00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
DateTime.checkValue("199/10/10 00:00:00", criteria) should be(Some(INVALID_NUMBER_ERROR_FOR_YEAR))
}

"checkValue should return an error if the day is not valid for given month" in {
DateTime.checkValue("1990-2-29T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990-4-31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990-6-31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990-9-31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990-11-31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990-11-31 00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/2/29T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/4/31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/6/31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/9/31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/11/31T00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
DateTime.checkValue("1990/11/31 00:00:00", criteria) should be(Some(INVALID_DAY_FOR_MONTH_ERROR))
}

"checkValue should return an error if future date is not allowed" in {
DateTime.checkValue("2050-2-1T00:00:00", criteria) should be(Some(FUTURE_DATE_ERROR))
DateTime.checkValue("2050/2/1T00:00:00", criteria) should be(Some(FUTURE_DATE_ERROR))
}
}

Expand Down

0 comments on commit 69688fa

Please sign in to comment.