Created
March 22, 2022 07:35
-
-
Save supermanue/81a513e9c5f92521fcde76f67c46fa93 to your computer and use it in GitHub Desktop.
Refined Types for users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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