Skip to content

Commit 9048497

Browse files
committed
Add example usage for string validators
1 parent 2a57714 commit 9048497

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/test/kotlin/TestServer.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import com.papsign.ktor.openapigen.annotations.type.number.integer.clamp.Clamp
3333
import com.papsign.ktor.openapigen.annotations.type.number.integer.max.Max
3434
import com.papsign.ktor.openapigen.annotations.type.number.integer.min.Min
3535
import com.papsign.ktor.openapigen.annotations.type.string.example.StringExample
36+
import com.papsign.ktor.openapigen.annotations.type.string.length.ConstraintViolation
37+
import com.papsign.ktor.openapigen.annotations.type.string.length.Length
38+
import com.papsign.ktor.openapigen.annotations.type.string.length.MaxLength
39+
import com.papsign.ktor.openapigen.annotations.type.string.length.MinLength
40+
import com.papsign.ktor.openapigen.annotations.type.string.pattern.RegularExpression
3641
import io.ktor.application.application
3742
import io.ktor.application.call
3843
import io.ktor.application.install
@@ -113,6 +118,12 @@ object TestServer {
113118
exception<ConstraintVialoation, Error>(HttpStatusCode.BadRequest) {
114119
Error("violation.constraint", it.localizedMessage)
115120
}
121+
exception<ConstraintViolation, Error>(HttpStatusCode.BadRequest) {
122+
Error("violation.constraint", it.localizedMessage)
123+
}
124+
exception<com.papsign.ktor.openapigen.annotations.type.string.pattern.ConstraintViolation, Error>(HttpStatusCode.BadRequest) {
125+
Error("violation.constraint", it.localizedMessage)
126+
}
116127
exception<ProperException, Error>(HttpStatusCode.BadRequest) {
117128
it.printStackTrace()
118129
Error(it.id, it.localizedMessage)
@@ -187,6 +198,18 @@ object TestServer {
187198
respond(LongResponse(params.a))
188199
}
189200

201+
route("validate-string").post<Unit, StringResponse, StringValidatorsExample>(
202+
info("This endpoint demonstrates the usage of String validators", "This endpoint demonstrates the usage of String validators"),
203+
exampleRequest = StringValidatorsExample(
204+
"A string that is at least 2 characters long",
205+
"A short string",
206+
"Between 2 and 20",
207+
"5a21be2"),
208+
exampleResponse = StringResponse("All of the fields were valid")
209+
) { params, body ->
210+
respond(StringResponse("All of the fields were valid"))
211+
}
212+
190213
route("again") {
191214
tag(TestServer.Tags.EXAMPLE) {
192215

@@ -231,6 +254,13 @@ object TestServer {
231254
@Response("A Response for header param example")
232255
data class NameGreetingResponse(@StringExample("Hi, John!") val str: String)
233256

257+
@Request("A Request with String fields validated for length or pattern")
258+
data class StringValidatorsExample(
259+
@MinLength(2,"Optional custom error message") val strWithMinLength: String,
260+
@MaxLength( 20 ) val strWithMaxLength: String,
261+
@Length(2, 20 ) val strWithLength: String,
262+
@RegularExpression("^[0-9a-fA-F]*$", "The field strHexaDec should only contain hexadecimal digits") val strHexaDec: String
263+
)
234264

235265
@Response("A String Response")
236266
@Request("A String Request")

0 commit comments

Comments
 (0)