Skip to content

Commit

Permalink
Remove iron integration trait scope restriction (#4111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sehnryr authored Oct 24, 2024
1 parent c77d501 commit 9b98cb7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ trait TapirCodecIron extends DescriptionWitness with LowPriorityValidatorForPred

}

private[iron] trait ValidatorForPredicate[Value, Predicate] {
trait ValidatorForPredicate[Value, Predicate] {
def validator: Validator[Value]
def makeErrors(value: Value, errorMessage: String): List[ValidationError[_]]
lazy val containsMinSizePositive: Boolean = validator.asPrimitiveValidators.exists {
Expand All @@ -218,12 +218,12 @@ private[iron] trait ValidatorForPredicate[Value, Predicate] {
}
}

private[iron] trait PrimitiveValidatorForPredicate[Value, Predicate] extends ValidatorForPredicate[Value, Predicate] {
trait PrimitiveValidatorForPredicate[Value, Predicate] extends ValidatorForPredicate[Value, Predicate] {
def validator: Validator.Primitive[Value]
def makeErrors(value: Value, errorMessage: String): List[ValidationError[_]]
}

private[iron] object ValidatorForPredicate {
object ValidatorForPredicate {
def fromPrimitiveValidator[Value, Predicate](
primitiveValidator: Validator.Primitive[Value]
): PrimitiveValidatorForPredicate[Value, Predicate] =
Expand All @@ -235,7 +235,7 @@ private[iron] object ValidatorForPredicate {
}

// #3938: the two-level low-priority validators are needed because of implicit resolution changes in Scala 3.6
private[iron] trait LowPriorityValidatorForPredicate extends LowPriorityValidatorForPredicate2 {
trait LowPriorityValidatorForPredicate extends LowPriorityValidatorForPredicate2 {

inline given validatorForDescribedAnd[N, P](using
id: IsDescription[P],
Expand Down
25 changes: 25 additions & 0 deletions integrations/iron/src/test/scala-3/com/example/RefinedString.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example

import io.github.iltotore.iron.*
import sttp.tapir.Validator
import sttp.tapir.codec.iron.PrimitiveValidatorForPredicate
import sttp.tapir.codec.iron.ValidatorForPredicate

final class RefinedStringConstraint

object RefinedStringConstraint {

given Constraint[String, RefinedStringConstraint] with {

override inline def test(value: String): Boolean = value.nonEmpty

override inline def message: String = "Should not be empty"
}

given PrimitiveValidatorForPredicate[String, RefinedStringConstraint] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.pattern[String]("^.+"))
}

opaque type RefinedString = String :| RefinedStringConstraint

object RefinedString extends RefinedTypeOps[String, RefinedStringConstraint, RefinedString]
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import io.github.iltotore.iron.constraint.all.*
import sttp.tapir.Validator
import sttp.tapir.ValidationError

import com.example.RefinedString
import com.example.RefinedStringConstraint

class TapirCodecIronTestScala3 extends AnyFlatSpec with Matchers {

val schema: Schema[Double :| Positive] = summon[Schema[Double :| Positive]]
Expand Down Expand Up @@ -321,4 +324,11 @@ class TapirCodecIronTestScala3 extends AnyFlatSpec with Matchers {
summon[Schema[NewtypeInt]]
summon[Codec[String, NewtypeInt, TextPlain]]

"Instances for opaque refined type defined outside of source" should "be correctly derived" in:
summon[Schema[RefinedString]]
summon[Codec[String, RefinedString, TextPlain]]

"Instance of validator for constraint defined outside of source" should "be correctly derived" in:
summon[PrimitiveValidatorForPredicate[String, RefinedStringConstraint]]

}

0 comments on commit 9b98cb7

Please sign in to comment.