-
Notifications
You must be signed in to change notification settings - Fork 42
Transformers and Validators (WIP: needs more standard transformers validators)
Wicpar edited this page Jan 13, 2020
·
1 revision
Inspired by Javax validators
Reflection is performed during the route build to create a list of transformers on the request payloads, at runtime the parameters are transformed by the built list of transformers.
(Transformers and validators can be applied to both Parameters and request Bodies)
@Request("Requests an invitation email")
data class EmailInvite(
@LowerCase // Apply the lowercase transformer
@Trim // Apply the trim transformer
val email: String // The received email will be lowercase and trimmed
)
...
// Then just create a route like always
route("invite") {
post<IDParam, EmailInviteResponse, EmailInvite, APIPrincipal> {params, invite ->
respond(EmailInviteResponse(EmailService.sendSignatureInvitationEmail(invite, params.processID, principal()).await()))
}
}
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY)
@ValidationAnnotation
annotation class LowerCase
object LowerCaseValidator: AValidator<String, LowerCase>(String::class, LowerCase::class) {
override fun validate(subject: String?, annotation: LowerCase): String? {
return subject?.toLowerCase() // Throw exception if the request is invalid, it will be caught by any appropriate exception handler that was declared on the route
}
}
install(OpenAPIGen) {
...
scanPackagesForModules += "com.example.validators"
}