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

Remove iron integration trait scope restriction #4111

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]]

}
Loading