Skip to content

Commit

Permalink
Iron: add validator for Pure constraint (#3818)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubbush authored Jun 5, 2024
1 parent 883d75c commit 08bd19c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ trait TapirCodecIron extends DescriptionWitness with LowPriorityValidatorForPred
inline given PrimitiveValidatorForPredicate[String, Not[Empty]] =
ValidatorForPredicate.fromPrimitiveValidator[String, Not[Empty]](Validator.minLength(1))

inline given validatorForPure[T]: ValidatorForPredicate[T, Pure] =
new ValidatorForPredicate[T, Pure] {
override val validator: Validator[T] = Validator.pass
override def makeErrors(value: T, errorMessage: String): List[ValidationError[_]] = Nil
}

inline given validatorForMatchesRegexpString[S <: String](using witness: ValueOf[S]): PrimitiveValidatorForPredicate[String, Match[S]] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.pattern[String](witness.value))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,9 @@ class TapirCodecIronTestScala3 extends AnyFlatSpec with Matchers {
summon[Schema[RefinedInt]]
summon[Codec[String, RefinedInt, TextPlain]]

"Instances for newtypes" should "be correctly derived" in:
type NewtypeInt = Int :| Pure
summon[Schema[NewtypeInt]]
summon[Codec[String, NewtypeInt, TextPlain]]

}

0 comments on commit 08bd19c

Please sign in to comment.