Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chaining refined string operation #1080

Open
aurrelhebert opened this issue Apr 4, 2022 · 1 comment · May be fixed by #1141
Open

Chaining refined string operation #1080

aurrelhebert opened this issue Apr 4, 2022 · 1 comment · May be fixed by #1141

Comments

@aurrelhebert
Copy link

aurrelhebert commented Apr 4, 2022

Context

Hi, I would like to use REFINED to validate some custom input string I have. For some of the string, I would like to chain existing refined string operation as start_with and uuid.

Exemple

The input string I have looks like random_VALID_UUID. At the moment I write a custom refined type as:

final case class PrefixedUUID[S](prefix: S)

object PrefixedUUID {

  implicit def prefixedUUIDValidate[S <: String](
      implicit
      prefix: Witness.Aux[S]
    ): Validate.Plain[String, PrefixedUUID[S]] = {
    Validate.fromPartial(
      s => {
        if (s.startsWith(prefix.value)) {
          val uuid: String = s.stripPrefix(prefix.value)
          require(java.util.UUID.fromString(uuid).toString == uuid.toLowerCase)
        } else {
          throw new Exception("Not a valid UUID")
        }
      },
      s"""PrefixedUUID("${prefix.value}")""",
      PrefixedUUID(prefix.value)
    )
  }
}

Is it the best approach to do it? Can I do it in a more generic way (chaining native refined operation)? If not, maybe can you lead me on how I can contribute a generic way to do it for the core refined library, if you are interested?

Thanks,

@aurrelhebert aurrelhebert linked a pull request Dec 6, 2022 that will close this issue
@aurrelhebert
Copy link
Author

aurrelhebert commented Jan 23, 2023

I think my first implementation is a bit limited. Maybe we could implement a more generic Split String function that will allow us to apply a list of predicates.

What I would see would be something like:

Split[N, SplitOp, List[P]]

where n would be the amount of expected splits (example: 2), a split operator (example: Count(2), By("_")) and a predicate list (example: UUID :: URL :: HNil).

We could even create a Partition alias operator that would be:

Partition[SplitOp, P1, P2] = Split[2, SplitOp, P1 :: P2 :: HNil]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant