Skip to content

Instantly share code, notes, and snippets.

@supermanue
Created March 22, 2022 07:35
Show Gist options
  • Save supermanue/81a513e9c5f92521fcde76f67c46fa93 to your computer and use it in GitHub Desktop.
Save supermanue/81a513e9c5f92521fcde76f67c46fa93 to your computer and use it in GitHub Desktop.
Refined Types for users
type Name = String Refined NameRestrictions
type Id = Int Refined IdRestrictions
//string between 1 and 1000 characters, not all of them white
type NameRestrictions = Size[Interval.Closed[W.`1`.T, W.`1000`.T]] And Not[Forall[Whitespace]]
type IdRestrictions = Positive
private def toRefinedId(id: Int): Either[RefinedTypeError, Id] =
refineV[IdRestrictions](id).left
.map(_ => RefinedTypeError("must be a positive int", id.toString))
private def toRefinedName(name: String): Either[RefinedTypeError, Name] =
refineV[NameRestrictions](name).left
.map(_ => RefinedTypeError("must be a string between 1 and 1000 characters, not all of them white", name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment