Skip to content

Commit

Permalink
drop validated support
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Aug 9, 2023
1 parent e9bb17b commit e120eea
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 37 deletions.
13 changes: 9 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ lazy val proto = project
.enablePlugins(Fs2Grpc)
.settings(
name := "spice4s-client-proto-api",
scalapbCodeGenerators := scalapbCodeGenerators.value ++ Seq(
scalapbCodeGenerators := scalapbCodeGenerators.value/* ++ Seq(
protocbridge.Target(scalapb.validate.gen(), (Compile / sourceManaged).value / "scalapb")
),
)*/,
libraryDependencies ++= Seq(
"com.thesamet.scalapb" %% "scalapb-validate-core" % scalapb.validate.compiler.BuildInfo.version % "protobuf",
/* "com.thesamet.scalapb" %% "scalapb-validate-core" % scalapb.validate.compiler.BuildInfo.version % "protobuf", */
"com.thesamet.scalapb.common-protos" %% "pgv-proto-scalapb_0.11" % "0.6.13-0",
"com.thesamet.scalapb.common-protos" %% "pgv-proto-scalapb_0.11" % "0.6.13-0" % "protobuf",
"com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.9.6-0" % "protobuf",
"com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.9.6-0"
),
tlFatalWarningsInCi := false
tlFatalWarningsInCi := false,
excludeDependencies ++= Seq(
ExclusionRule("com.thesamet.scalapb", "scalapb-validate-core_2.13"),
)
)

lazy val client = project
Expand Down
21 changes: 0 additions & 21 deletions client/src/main/scala/spice4s/client/Touch.scala

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/main/scala/spice4s/client/models/Limit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ object Limit {

def apply(value: Int): Validation[Limit] =
if (value >= 0 || value <= 1000) unsafeFromString(value).valid
else invalid("limit", value, "Limit must be between 0 and 1000")
else invalid("limit", "Limit must be between 0 and 1000")
}
3 changes: 2 additions & 1 deletion client/src/main/scala/spice4s/client/models/ZedToken.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ object ZedToken {
def unsafeFromString(token: String): ZedToken = new ZedToken(token) {}

def apply(token: String): Validation[ZedToken] =
unsafeFromString(token).validator(_.encode)
if (token.size >= 1) unsafeFromString(token).valid
else invalid("zed-token", "ZedToken must be at least 1 character long")

def decode(x: core.ZedToken): Decoded[Option[ZedToken]] =
opt(x.token).map(unsafeFromString).pure[Decoded]
Expand Down
20 changes: 11 additions & 9 deletions client/src/main/scala/spice4s/client/util/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,36 @@
package spice4s.client

import cats._
import scalapb.validate._
import cats.data._
import cats.implicits._
import scala.util.matching.Regex
import cats.ApplicativeError

package object util {
final case class ValidationFailure(name: String, message: String)

implicit class AnyUtilOps[A](private val value: A) {
def validator[V: Validator](f: A => V): Validated[List[ValidationFailure], A] =
Validator[V].validate(f(value)) match {
case Failure(violations) => violations.invalid
case Success => value.valid
}
def minSize(size: Int)(f: A => String): Validated[List[ValidationFailure], A] =
if (f(value).length >= size) value.valid
else
invalid(
"min-size-field",
s"'$value' with encoded value ${f(value)} is less than min size $size"
)

def validateRegex(regex: Regex)(f: A => String): Validated[List[ValidationFailure], A] =
if (regex.matches(f(value))) value.valid
else
invalid(
"regex-field",
f(value),
s"$value with encoded value ${f(value)} does not match regex ${regex.toString}"
)
}

type Validation[A] = Validated[List[ValidationFailure], A]

def invalid[A](name: String, a: Any, reason: String): Validation[A] =
List(ValidationFailure(name, a.toString, reason)).invalid
def invalid[A](name: String, message: String): Validation[A] =
List(ValidationFailure(name, message)).invalid

final case class DecoderError(message: String, path: Chain[String]) {
def prepend(p: String): DecoderError = copy(path = p +: path)
Expand Down
1 change: 1 addition & 0 deletions project/metals.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// This file enables sbt-bloop to create bloop config files.

addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.6")

2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ libraryDependencies ++= Seq(
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.11",
"com.thesamet.scalapb" %% "scalapb-validate-codegen" % "0.3.4"
)

addDependencyTreePlugin
9 changes: 9 additions & 0 deletions proto/src/main/protobuf/authzed/api/v1/package.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
import "scalapb/scalapb.proto";

package google.api;

option (scalapb.options) = {
scope: PACKAGE
flat_package: false
};
9 changes: 9 additions & 0 deletions proto/src/main/protobuf/authzed/api/v1/package2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
import "scalapb/scalapb.proto";

package authzed.api;

option (scalapb.options) = {
scope: PACKAGE
flat_package: false
};
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,4 @@ message ResolvedSubject {

// partial_caveat_info holds information of a partially-evaluated caveated response
PartialCaveatInfo partial_caveat_info = 3 [ (validate.rules).message.required = false ];
}
}

0 comments on commit e120eea

Please sign in to comment.