Forked from the original library (here) Updated dependencies:
- Added support to scala 2.13
- Scalatest 3.2.7
- pegdown 1.6.0
- scalaCheck 1.14.1
- added: org.scalatestplus-scalacheck
- Removed : Scala 2.11 and 2.12 ( because of libs incompatibilities )
- Removed : dependency to "SbtAutoBuildPlugin" (specific sbt template from https://dl.bintray.com/hmrc/sbt-plugin-releases)
Scala micro-library for typing, validating and obfuscating email addresses
The EmailAddress
class will only accept valid addresses:
scala> import uk.gov.hmrc.emailaddress._
import uk.gov.hmrc.emailaddress._
scala> EmailAddress("[email protected]")
res0: uk.gov.hmrc.emailaddress.EmailAddress = example@test.com
scala> EmailAddress("not_a_meaningful_address")
java.lang.IllegalArgumentException: requirement failed: 'not_a_meaningful_address' is not a valid email address
You can also use EmailAddress.isValid(...)
:
scala> EmailAddress.isValid("[email protected]")
res2: Boolean = true
scala> EmailAddress.isValid("not_a_meaningful_address")
res3: Boolean = false
You can access the mailbox and domain of a given address:
scala> EmailAddress("[email protected]").domain
res0: uk.gov.hmrc.emailaddress.EmailAddress.Domain = test.com
scala> EmailAddress("[email protected]").mailbox
res1: uk.gov.hmrc.emailaddress.EmailAddress.Mailbox = example
These compare equal as you might expect:
scala> EmailAddress("[email protected]").domain == EmailAddress("[email protected]").domain
res2: Boolean = true
scala> EmailAddress("[email protected]").domain == EmailAddress("[email protected]").domain
res3: Boolean = false
Addresses are obfuscated by starring out all of their mailbox part, apart from the first and last letters:
scala> ObfuscatedEmailAddress("[email protected]")
res4: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = e*****e@test.com
Unless there are only two letters:
scala> ObfuscatedEmailAddress("[email protected]")
res7: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = **@test.com```
You can also create them directly from an EmailAddress
:
scala> EmailAddress("[email protected]").obfuscated
res6: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = e*****e@test.com
All classes toString
and implicitly convert to String
s nicely:
scala> val someString: String = EmailAddress("[email protected]")
someString: String = example@test.com
scala> val someString = EmailAddress("[email protected]").toString
someString: String = example@test.com
scala> val someString: String = ObfuscatedEmailAddress("[email protected]")
someString: String = e*****e@test.com
scala> val someString = ObfuscatedEmailAddress("[email protected]").toString
someString: String = e*****e@test.com
scala> EmailAddress("[email protected]").domain.toString
res4: String = test.com
scala> val s: String = EmailAddress("[email protected]").domain
s: String = test.com
scala> EmailAddress("[email protected]").mailbox.toString
res5: String = example
scala> val s: String = EmailAddress("[email protected]").mailbox
s: String = example
This code is open source software licensed under the Apache 2.0 License.