Skip to content

Commit

Permalink
add tests for type and constraint defined outside of source
Browse files Browse the repository at this point in the history
  • Loading branch information
sehnryr committed Oct 24, 2024
1 parent da74812 commit c77cb68
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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 c77cb68

Please sign in to comment.