Skip to content

Commit

Permalink
Close #288 - [refined4s-tapir] Add tapir support for NonBlankString
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lee committed Apr 9, 2024
1 parent 6c154ff commit a9b5459
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
@@ -1,6 +1,6 @@
package refined4s.modules.tapir.derivation.types

import refined4s.types.strings.{NonEmptyString, Uuid}
import refined4s.types.strings.{NonBlankString, NonEmptyString, Uuid}
import sttp.tapir.Schema

import java.util.UUID
Expand All @@ -12,13 +12,18 @@ trait strings {

inline given schemaNonEmptyString: Schema[NonEmptyString] = strings.derivedNonEmptyStringSchema

inline given schemaNonBlankString: Schema[NonBlankString] = strings.derivedNonBlankStringSchema

inline given schemaUuid: Schema[Uuid] = strings.derivedUuidSchema

}
object strings {
given derivedNonEmptyStringSchema: Schema[NonEmptyString] =
summon[Schema[String]].map(NonEmptyString.from(_).toOption)(_.value)

given derivedNonBlankStringSchema: Schema[NonBlankString] =
summon[Schema[String]].map(NonBlankString.from(_).toOption)(_.value)

given derivedUuidSchema: Schema[Uuid] =
summon[Schema[UUID]].map(uuid => Some(Uuid.apply(uuid)))(_.toUUID)
}
Expand Up @@ -8,6 +8,7 @@ import refined4s.types.all.*
import refined4s.types.networkGens
import sttp.tapir.{Schema, ValidationError}

import java.nio.charset.StandardCharsets
import java.util.UUID

/** @author Kevin Lee
Expand Down Expand Up @@ -82,6 +83,8 @@ object autoSpec extends Properties {
//
property("test Schema[NonEmptyString]", testSchemaNonEmptyString),
//
property("test Schema[NonBlankString]", testSchemaNonBlankString),
//
property("test Schema[Uuid]", testSchemaUuid),
//
property("test Schema[Uri]", testSchemaUri),
Expand Down Expand Up @@ -502,6 +505,29 @@ object autoSpec extends Properties {
actual ==== expected
}

def testSchemaNonBlankString: Property =
for {
nonWhitespaceString <- Gen
.string(hedgehog.extra.Gens.genNonWhitespaceChar, Range.linear(1, 10))
.map(s => new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8))
.log("nonWhitespaceString")
whitespaceString <- Gen
.string(
hedgehog.extra.Gens.genCharByRange(refined4s.types.strings.WhitespaceCharRange),
Range.linear(1, 10),
)
.log("whitespaceString")

s <- Gen.constant(scala.util.Random.shuffle((nonWhitespaceString + whitespaceString).toList).mkString).log("s")
} yield {
val input = NonBlankString.unsafeFrom(s)

val expected = List.empty[ValidationError[?]]
val actual = summon[Schema[NonBlankString]].applyValidation(input)

actual ==== expected
}

def testSchemaUuid: Property =
for {
uuid <- Gen.constant(UUID.randomUUID()).log("uuid")
Expand Down
Expand Up @@ -7,6 +7,7 @@ import refined4s.types.all.*
import refined4s.types.networkGens
import sttp.tapir.{Schema, ValidationError}

import java.nio.charset.StandardCharsets
import java.util.UUID

/** @author Kevin Lee
Expand Down Expand Up @@ -81,6 +82,8 @@ object allSpec extends Properties {
//
property("test Schema[NonEmptyString]", testSchemaNonEmptyString),
//
property("test Schema[NonBlankString]", testSchemaNonBlankString),
//
property("test Schema[Uuid]", testSchemaUuid),
//
property("test Schema[Uri]", testSchemaUri),
Expand Down Expand Up @@ -497,6 +500,29 @@ object allSpec extends Properties {
actual ==== expected
}

def testSchemaNonBlankString: Property =
for {
nonWhitespaceString <- Gen
.string(hedgehog.extra.Gens.genNonWhitespaceChar, Range.linear(1, 10))
.map(s => new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8))
.log("nonWhitespaceString")
whitespaceString <- Gen
.string(
hedgehog.extra.Gens.genCharByRange(refined4s.types.strings.WhitespaceCharRange),
Range.linear(1, 10),
)
.log("whitespaceString")

s <- Gen.constant(scala.util.Random.shuffle((nonWhitespaceString + whitespaceString).toList).mkString).log("s")
} yield {
val input = NonBlankString.unsafeFrom(s)

val expected = List.empty[ValidationError[?]]
val actual = summon[Schema[NonBlankString]].applyValidation(input)

actual ==== expected
}

def testSchemaUuid: Property =
for {
uuid <- Gen.constant(UUID.randomUUID()).log("uuid")
Expand Down

0 comments on commit a9b5459

Please sign in to comment.