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

Refined: Treat fields with NonEmpty or MinSize(>0) as required #3839

Merged
merged 2 commits into from
Jun 13, 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 @@ -8,6 +8,7 @@ import eu.timepit.refined.numeric.{Greater, GreaterEqual, Less, LessEqual}
import eu.timepit.refined.refineV
import eu.timepit.refined.string.{MatchesRegex, Uuid}
import sttp.tapir._
import sttp.tapir.internal._

import scala.reflect.ClassTag

Expand All @@ -19,6 +20,17 @@ trait TapirCodecRefined extends LowPriorityValidatorForPredicate {
): Schema[V Refined P] =
vSchema.validate(refinedValidatorTranslation.validator).map[V Refined P](v => refineV[P](v).toOption)(_.value)

implicit def refinedTapirSchemaIterable[X, C[X] <: Iterable[X], P](implicit
vSchema: Schema[C[X]],
refinedValidator: Validate[C[X], P],
refinedValidatorTranslation: ValidatorForPredicate[C[X], P]
): Schema[C[X] Refined P] = {
vSchema
.validate(refinedValidatorTranslation.validator)
.map[C[X] Refined P](v => refineV[P](v).toOption)(_.value)
.copy(isOptional = if (refinedValidatorTranslation.containsMinSizePositive) false else vSchema.isOptional)
}

implicit def codecForRefined[R, V, P, CF <: CodecFormat](implicit
tm: Codec[R, V, CF],
refinedValidator: Validate[V, P],
Expand Down Expand Up @@ -62,6 +74,19 @@ trait TapirCodecRefined extends LowPriorityValidatorForPredicate {
): PrimitiveValidatorForPredicate[T, MinSize[NM]] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.minLength[T](ws.snd))

implicit def validatorForNonEmptyOnIterable[X, T[X] <: Iterable[X]]: PrimitiveValidatorForPredicate[T[X], NonEmpty] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.minSize[X, T](1))

implicit def validatorForMinSizeOnIterable[X, T[X] <: Iterable[X], NM](implicit
ws: WitnessAs[NM, Int]
): PrimitiveValidatorForPredicate[T[X], MinSize[NM]] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.minSize[X, T](ws.snd))

implicit def validatorForMaxSizeOnIterable[X, T[X] <: Iterable[X], NM](implicit
ws: WitnessAs[NM, Int]
): PrimitiveValidatorForPredicate[T[X], MaxSize[NM]] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.maxSize[X, T](ws.snd))

implicit def validatorForLess[N: Numeric, NM](implicit ws: WitnessAs[NM, N]): PrimitiveValidatorForPredicate[N, Less[NM]] =
ValidatorForPredicate.fromPrimitiveValidator(Validator.max(ws.snd, exclusive = true))

Expand Down Expand Up @@ -133,6 +158,10 @@ trait TapirCodecRefined extends LowPriorityValidatorForPredicate {

trait ValidatorForPredicate[V, P] {
def validator: Validator[V]
lazy val containsMinSizePositive: Boolean = validator.asPrimitiveValidators.exists {
case Validator.MinSize(a) => a > 0
case _ => false
}
def validationErrors(value: V, refinedErrorMessage: String): List[ValidationError[_]]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sttp.tapir.codec.refined

import eu.timepit.refined.api.Refined
import eu.timepit.refined.boolean.Or
import eu.timepit.refined.boolean._
import eu.timepit.refined.collection.{MaxSize, MinSize, NonEmpty}
import eu.timepit.refined.numeric.{Greater, GreaterEqual, Interval, Less, LessEqual}
import eu.timepit.refined.string.MatchesRegex
Expand Down Expand Up @@ -159,6 +159,17 @@ class TapirCodecRefinedTestScala2 extends AnyFlatSpec with Matchers with TapirCo
}
}

"Generated schema for NonEmpty and MinSize" should "not be optional" in {
assert(implicitly[Schema[List[Int]]].isOptional)
assert(!implicitly[Schema[List[Int] Refined NonEmpty]].isOptional)
assert(!implicitly[Schema[Set[Int] Refined NonEmpty]].isOptional)
assert(!implicitly[Schema[List[Int] Refined MinSize[W.`3`.T]]].isOptional)
assert(!implicitly[Schema[List[Int] Refined (MinSize[W.`3`.T] And MaxSize[W.`6`.T])]].isOptional)
assert(implicitly[Schema[List[Int] Refined MinSize[W.`0`.T]]].isOptional)
assert(implicitly[Schema[List[Int] Refined MaxSize[W.`5`.T]]].isOptional)
assert(implicitly[Schema[Option[List[Int] Refined NonEmpty]]].isOptional)
}

"TapirCodecRefined" should "compile using implicit schema for refined types" in {
import io.circe.refined._
import sttp.tapir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sttp.tapir.codec.refined

import eu.timepit.refined.api.Refined
import eu.timepit.refined.boolean.Or
import eu.timepit.refined.boolean.*
import eu.timepit.refined.collection.{MaxSize, MinSize, NonEmpty}
import eu.timepit.refined.numeric.{Greater, GreaterEqual, Interval, Less, LessEqual, Negative, NonNegative, NonPositive, Positive}
import eu.timepit.refined.string.{IPv4, MatchesRegex}
import eu.timepit.refined.numeric.{Greater, GreaterEqual, Interval, Less, LessEqual}
import eu.timepit.refined.string.MatchesRegex
import eu.timepit.refined.types.string.NonEmptyString
import eu.timepit.refined.refineV
import org.scalatest.flatspec.AnyFlatSpec
Expand Down Expand Up @@ -161,6 +161,17 @@ class TapirCodecRefinedTestScala3 extends AnyFlatSpec with Matchers with TapirCo
case Validator.Mapped(Validator.Any(List(Validator.Min(3, true), Validator.Max(-3, true))), _) =>
}
}

"Generated schema for NonEmpty and MinSize" should "not be optional" in {
assert(implicitly[Schema[List[Int]]].isOptional)
assert(!implicitly[Schema[List[Int] Refined NonEmpty]].isOptional)
assert(!implicitly[Schema[Set[Int] Refined NonEmpty]].isOptional)
assert(!implicitly[Schema[List[Int] Refined MinSize[3]]].isOptional)
assert(!implicitly[Schema[List[Int] Refined (MinSize[3] And MaxSize[6])]].isOptional)
assert(implicitly[Schema[List[Int] Refined MinSize[0]]].isOptional)
assert(implicitly[Schema[List[Int] Refined MaxSize[5]]].isOptional)
assert(implicitly[Schema[Option[List[Int] Refined NonEmpty]]].isOptional)
}

"TapirCodecRefined" should "compile using implicit schema for refined types" in {
import io.circe.refined._
Expand Down
Loading