|
| 1 | +package sttp.tapir.codec.refined |
| 2 | + |
| 3 | +import eu.timepit.refined.api.{Max, Refined} |
| 4 | +import eu.timepit.refined.collection.NonEmpty |
| 5 | +import eu.timepit.refined.numeric.{Greater, GreaterEqual, Less, LessEqual} |
| 6 | +import eu.timepit.refined.string.{IPv4, MatchesRegex} |
| 7 | +import eu.timepit.refined.{W, refineMV, refineV} |
| 8 | +import eu.timepit.refined.types.string.NonEmptyString |
| 9 | +import org.scalatest.{FlatSpec, Matchers} |
| 10 | +import sttp.tapir.Codec.PlainCodec |
| 11 | +import sttp.tapir.{DecodeResult, ValidationError, Validator} |
| 12 | + |
| 13 | +class TapirCodecRefinedTest extends FlatSpec with Matchers with TapirCodecRefined { |
| 14 | + |
| 15 | + val nonEmptyStringCodec = implicitly[PlainCodec[NonEmptyString]] |
| 16 | + |
| 17 | + |
| 18 | + "Generated codec" should "return DecodResult.Invalid if subtype can't be refined with correct tapir validator if available" in { |
| 19 | + val expectedValidator: Validator[String] = Validator.minLength(1) |
| 20 | + nonEmptyStringCodec.decode("") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, "", _))) if validator == expectedValidator=>} |
| 21 | + } |
| 22 | + |
| 23 | + it should "correctly delegate to raw parser and refine it" in { |
| 24 | + nonEmptyStringCodec.decode("vive le fromage") shouldBe DecodeResult.Value(refineMV[NonEmpty]("vive le fromage")) |
| 25 | + } |
| 26 | + |
| 27 | + it should "return DecodResult.Invalid if subtype can't be refined with derived tapir validator if non tapir validator available" in { |
| 28 | + type IPString = String Refined IPv4 |
| 29 | + val IPStringCodec = implicitly[PlainCodec[IPString]] |
| 30 | + |
| 31 | + val expectedMsg = refineV[IPv4]("192.168.0.1000").left.get |
| 32 | + IPStringCodec.decode("192.168.0.1000") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(Validator.Custom(_, `expectedMsg`), "192.168.0.1000", _)))=>} |
| 33 | + } |
| 34 | + |
| 35 | + "Generated codec for MatchesRegex" should "use tapir Validator.Pattern" in { |
| 36 | + type VariableConstraint = MatchesRegex[W.`"[a-zA-Z][-a-zA-Z0-9_]*"`.T] |
| 37 | + type VariableString = String Refined VariableConstraint |
| 38 | + val identifierCodec = implicitly[PlainCodec[VariableString]] |
| 39 | + |
| 40 | + val expectedValidator: Validator[String] = Validator.pattern("[a-zA-Z][-a-zA-Z0-9_]*") |
| 41 | + identifierCodec.decode("-bad") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, "-bad", _))) if validator == expectedValidator=>} |
| 42 | + } |
| 43 | + |
| 44 | + "Generated codec for Less" should "use tapir Validator.drMax" in { |
| 45 | + type IntConstraint = Less[W.`3`.T] |
| 46 | + type LimitedInt = Int Refined IntConstraint |
| 47 | + val limitedIntCodec = implicitly[PlainCodec[LimitedInt]] |
| 48 | + |
| 49 | + val expectedValidator: Validator[Int] = Validator.max(3, exclusive = true) |
| 50 | + limitedIntCodec.decode("3") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, 3, _))) if validator == expectedValidator=>} |
| 51 | + } |
| 52 | + |
| 53 | + "Generated codec for LessEqual" should "use tapir Validator.drMax" in { |
| 54 | + type IntConstraint = LessEqual[W.`3`.T] |
| 55 | + type LimitedInt = Int Refined IntConstraint |
| 56 | + val limitedIntCodec = implicitly[PlainCodec[LimitedInt]] |
| 57 | + |
| 58 | + val expectedValidator: Validator[Int] = Validator.max(3, exclusive = false) |
| 59 | + limitedIntCodec.decode("4") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, 4, _))) if validator == expectedValidator=>} |
| 60 | + } |
| 61 | + |
| 62 | + "Generated codec for Max" should "use tapir Validator.drMax" in { |
| 63 | + type IntConstraint = Greater[W.`3`.T] |
| 64 | + type LimitedInt = Int Refined IntConstraint |
| 65 | + val limitedIntCodec = implicitly[PlainCodec[LimitedInt]] |
| 66 | + |
| 67 | + val expectedValidator: Validator[Int] = Validator.min(3, exclusive = true) |
| 68 | + limitedIntCodec.decode("3") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, 3, _))) if validator == expectedValidator=>} |
| 69 | + } |
| 70 | + |
| 71 | + "Generated codec for MaxEqual" should "use tapir Validator.drMax" in { |
| 72 | + type IntConstraint = GreaterEqual[W.`3`.T] |
| 73 | + type LimitedInt = Int Refined IntConstraint |
| 74 | + val limitedIntCodec = implicitly[PlainCodec[LimitedInt]] |
| 75 | + |
| 76 | + val expectedValidator: Validator[Int] = Validator.min(3, exclusive = false) |
| 77 | + limitedIntCodec.decode("2") should matchPattern{case DecodeResult.InvalidValue(List(ValidationError(validator, 2, _))) if validator == expectedValidator=>} |
| 78 | + } |
| 79 | +} |
0 commit comments