Skip to content

A light-weight Scala library for Email validation.

License

Notifications You must be signed in to change notification settings

cyril-zx-chen/Smail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

S-Mail

A light-weight Scala library for Email validation.

Getting Started

Validate Email

import smail._

val validate: Boolean = Smail.isValid("[email protected]")
val toSmail: Either[SmailError, Smail] = Smail.from("[email protected]")
val unsafeToSmail: Smail = Smail.unsafeFrom("[email protected]")

To enable extension methods, Simply import smail.syntax._

import smail._
import smail.syntax._

val validatedWithSyntax: Boolean = "[email protected]".isValid
val toSmailWithSyntax: Either[SmailError, Smail] = "[email protected]".toSmail
val unsafeToSmailWithSyntax: Smail = "[email protected]".unsafeToSmail

Break Down Email

class Smail is protected with refined type NonEmptyString, which means once you get a valid Smail, you don't need to worry about empty strings in any part of an Email

import eu.timepit.refined.types.all.NonEmptyString
import smail._
import smail.syntax._

val mySmail: Smail = "[email protected]".unsafeToSmail
val emailName: NonEmptyString = mySmail.name // NonEmptyString("hi_smail") 
val emailDomain: NonEmptyString = mySmail.domain // NonEmptyString("gmail")
val emailTld: NonEmptyString = mySmail.tld // NonEmptyString("com")

val emailNameString: String = mySmail.name.value
val emailDomainString: String = mySmail.domain.value
val emailTldString: String = mySmail.tld.value

Error Handling

With EmailAddress, you can get the structured information for each part of the Email, i.e name(hi_smail)/domain(gmail, hotmail, outlook)/tld(com)

EmailParseError is a sealed trait with possible pattern matchings:

  • InvalidUserName: UserName is not empty but is invalid
  • InvalidDomain: Domain is not empty but is invalid
  • InvalidTopLevelDomain: TLD is not empty but is invalid
  • MissingField: any one or more of Username/Domain/TLD is missing

About

A light-weight Scala library for Email validation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages