diff options
author | Zach Smith <zach@driver.xyz> | 2018-07-19 15:57:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-19 15:57:53 +0800 |
commit | 819c8d14ed32785995ba8a42bdac80a8d9f2c557 (patch) | |
tree | 616d3b012d8c74c39772d87f93a912bcacbd621e /src/main/scala/xyz/driver/core | |
parent | 31cbc4b16751c9ed900ce04d932213b3f0dc961e (diff) | |
download | driver-core-1.11.8.tar.gz driver-core-1.11.8.tar.bz2 driver-core-1.11.8.zip |
Validate phone numbers when parsing from JSON (#183)v1.11.8
* Validate phone numbers when parsing from JSON
* Change to intercept for test
Diffstat (limited to 'src/main/scala/xyz/driver/core')
-rw-r--r-- | src/main/scala/xyz/driver/core/domain.scala | 10 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/json.scala | 8 |
2 files changed, 9 insertions, 9 deletions
diff --git a/src/main/scala/xyz/driver/core/domain.scala b/src/main/scala/xyz/driver/core/domain.scala index fa3b5c4..59bed54 100644 --- a/src/main/scala/xyz/driver/core/domain.scala +++ b/src/main/scala/xyz/driver/core/domain.scala @@ -32,14 +32,8 @@ object domain { private val phoneUtil = PhoneNumberUtil.getInstance() def parse(phoneNumber: String): Option[PhoneNumber] = { - val phone = scala.util.Try(phoneUtil.parseAndKeepRawInput(phoneNumber, "US")).toOption - - val validated = phone match { - case None => None - case Some(pn) => - if (!phoneUtil.isValidNumber(pn)) None - else Some(pn) - } + val validated = + util.Try(phoneUtil.parseAndKeepRawInput(phoneNumber, "US")).toOption.filter(phoneUtil.isValidNumber) validated.map(pn => PhoneNumber(pn.getCountryCode.toString, pn.getNationalNumber.toString)) } } diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala index de1df31..959239e 100644 --- a/src/main/scala/xyz/driver/core/json.scala +++ b/src/main/scala/xyz/driver/core/json.scala @@ -186,7 +186,13 @@ object json { } } - implicit val phoneNumberFormat = jsonFormat2(PhoneNumber.apply) + implicit object phoneNumberFormat extends RootJsonFormat[PhoneNumber] { + private val basicFormat = jsonFormat2(PhoneNumber.apply) + override def write(obj: PhoneNumber): JsValue = basicFormat.write(obj) + override def read(json: JsValue): PhoneNumber = { + PhoneNumber.parse(basicFormat.read(json).toString).getOrElse(deserializationError("Invalid phone number")) + } + } implicit val authCredentialsFormat = new RootJsonFormat[AuthCredentials] { override def read(json: JsValue): AuthCredentials = { |