Skip to content

Commit

Permalink
Improve validation in uuid unmarshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippus committed Jul 7, 2019
1 parent 8b2afcb commit 3c7b573
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class UnmarshallingSpec extends FreeSpec with Matchers with BeforeAndAfterAll wi
val uuid = UUID.randomUUID()
Unmarshal(uuid.toString).to[UUID] should evaluateTo(uuid)
}
"uuidUnmarshaller should unmarshal nil uuid" in {
Unmarshal("00000000-0000-0000-0000-000000000000").to[UUID] should evaluateTo(UUID.fromString("00000000-0000-0000-0000-000000000000"))
}
}

"The GenericUnmarshallers" - {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ trait PredefinedFromStringUnmarshallers {
}
}

implicit val uuidFromStringUnmarshaller: Unmarshaller[String, UUID] =
Unmarshaller.strict { string =>
try UUID.fromString(string)
catch {
case e: IllegalArgumentException =>
throw new IllegalArgumentException(s"'$string' is not a valid UUID value", e)
}
implicit val uuidFromStringUnmarshaller: Unmarshaller[String, UUID] = {
val validUuidPattern =
"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000".r.pattern

Unmarshaller.strict[String, UUID] { string =>
if (validUuidPattern.matcher(string).matches)
UUID.fromString(string)
else
throw new IllegalArgumentException(s"'$string' is not a valid UUID value")
}
}

implicit def CsvSeq[T](implicit unmarshaller: Unmarshaller[String, T]): Unmarshaller[String, immutable.Seq[T]] =
Unmarshaller.strict[String, immutable.Seq[String]] { string =>
Expand Down

0 comments on commit 3c7b573

Please sign in to comment.