-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically validate request body using
Schema.validate
(#2360)
feat(endpoint): automatically validate body Uses the `Schema.validate` method from `zio-schema` to validate the body of a request based on the schema of the input type. If schema validation fails, `HttpCodecError.InvalidEntity` is passed to the error channel, indicating that the body is well-formed, but contains invalid values in one or more fields. A `wrap` method is included for `InvalidEntity` which simplifies the process of converting `Schema.validate`'s output (`Chunk[ValidationError]`) to `InvalidEntity` in a consistent way. resolves #2274
- Loading branch information
1 parent
fa76fe1
commit 37fe777
Showing
4 changed files
with
148 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
zio-http/src/test/scala/zio/http/codec/internal/BodyCodecSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package zio.http.codec.internal | ||
|
||
import zio._ | ||
import zio.test._ | ||
|
||
import zio.stream.{ZSink, ZStream} | ||
|
||
import zio.schema._ | ||
import zio.schema.annotation.validate | ||
import zio.schema.validation.Validation | ||
|
||
import zio.http.codec.HttpCodecError | ||
|
||
object BodyCodecSpec extends ZIOSpecDefault { | ||
import BodyCodec._ | ||
|
||
case class User( | ||
@validate(Validation.greaterThan(0)) | ||
id: Int, | ||
@validate(Validation.minLength(2) && Validation.maxLength(64)) | ||
name: String, | ||
) | ||
object User { | ||
val schema: Schema[User] = DeriveSchema.gen[User] | ||
} | ||
|
||
def spec = suite("BodyCodecSpec")( | ||
suite("validateZIO")( | ||
test("returns a valid entity") { | ||
val valid = User(12, "zio") | ||
|
||
for { | ||
actual <- validateZIO(User.schema)(valid) | ||
} yield assertTrue(valid == actual) | ||
} + | ||
test("fails with HttpCodecError for invalid entity") { | ||
val invalid = User(-4, "z") | ||
val validated = BodyCodec.validateZIO(User.schema)(invalid) | ||
|
||
assertZIO(validated.exit)(Assertion.failsWithA[HttpCodecError.InvalidEntity]) | ||
}, | ||
), | ||
suite("validateStream")( | ||
test("returns all valid entities") { | ||
val users = Chunk( | ||
User(1, "Will"), | ||
User(2, "Ammon"), | ||
) | ||
val valids = ZStream.fromChunk(users) | ||
|
||
for { | ||
validatedUsers <- valids.via(validateStream(User.schema)).runCollect | ||
} yield assertTrue(validatedUsers == users) | ||
}, | ||
test("fails with HttpCodecError for invalid entity") { | ||
val users = Chunk( | ||
User(1, "Will"), | ||
User(-5, "Ammon"), | ||
) | ||
val invalid = ZStream.fromChunk(users) | ||
|
||
for { | ||
validatedUsers <- invalid.via(validateStream(User.schema)).runCollect.exit | ||
} yield assert(validatedUsers)(Assertion.failsWithA[HttpCodecError.InvalidEntity]) | ||
}, | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters