Skip to content

Commit

Permalink
feature: add url and uri schemas (#302)
Browse files Browse the repository at this point in the history
Co-authored-by: John A. De Goes <[email protected]>
  • Loading branch information
tusharmath and jdegoes authored Aug 18, 2022
1 parent a398cd9 commit 710e754
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions zio-schema/shared/src/main/scala/zio/schema/Schema.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zio.schema

import java.net.{ URI, URL }
import java.time.temporal.ChronoUnit

import scala.collection.immutable.ListMap
Expand Down Expand Up @@ -271,6 +272,24 @@ object Schema extends SchemaEquality {
implicit def vector[A](implicit element: Schema[A]): Schema[Vector[A]] =
chunk(element).transform(_.toVector, Chunk.fromIterable(_))

implicit val url: Schema[java.net.URL] =
Schema[String].transformOrFail(
string =>
try {
Right(new URL(string))
} catch { case _: Exception => Left(s"Invalid URL: $string") },
url => Right(url.toString)
)

implicit val uri: Schema[java.net.URI] =
Schema[String].transformOrFail(
string =>
try {
Right(new URI(string))
} catch { case _: Exception => Left(s"Invalid URI: $string") },
uri => Right(uri.toString)
)

sealed trait Enum[A] extends Schema[A] {
def id: TypeId

Expand Down

0 comments on commit 710e754

Please sign in to comment.