From 32f62ec5abab2131c70f0924298b5cbba26eb63d Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 14 Apr 2021 23:53:19 +0200 Subject: [PATCH] Add validation to notification --- .../dcproject/component/notification/Notification.kt | 10 ++++++++++ .../component/notification/NotificationsPush.kt | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/component/notification/Notification.kt b/src/main/kotlin/fr/dcproject/component/notification/Notification.kt index 57e4320..df6aedb 100644 --- a/src/main/kotlin/fr/dcproject/component/notification/Notification.kt +++ b/src/main/kotlin/fr/dcproject/component/notification/Notification.kt @@ -11,6 +11,8 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import fr.dcproject.common.entity.Entity import fr.dcproject.component.article.database.ArticleForView +import io.konform.validation.Validation +import io.konform.validation.jsonschema.enum import org.joda.time.DateTime import java.util.concurrent.atomic.AtomicInteger @@ -50,6 +52,14 @@ open class Notification( inline fun fromString(raw: String): T = mapper.readValue(raw) } + + fun getValidation() = Validation { + Notification::type { + enum( + "article" + ) + } + } } open class EntityNotification( diff --git a/src/main/kotlin/fr/dcproject/component/notification/NotificationsPush.kt b/src/main/kotlin/fr/dcproject/component/notification/NotificationsPush.kt index 445cca1..78c9a43 100644 --- a/src/main/kotlin/fr/dcproject/component/notification/NotificationsPush.kt +++ b/src/main/kotlin/fr/dcproject/component/notification/NotificationsPush.kt @@ -1,6 +1,7 @@ package fr.dcproject.component.notification import com.fasterxml.jackson.core.JsonProcessingException +import fr.dcproject.application.http.badRequestIfNotValid import fr.dcproject.component.auth.citizen import fr.dcproject.component.citizen.database.CitizenI import io.ktor.http.cio.websocket.Frame @@ -28,7 +29,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.slf4j.LoggerFactory -class NotificationsPush( +class NotificationsPush ( private val redis: RedisAsyncCommands, private val redisConnectionPubSub: StatefulRedisPubSubConnection, citizen: CitizenI, @@ -52,7 +53,10 @@ class NotificationsPush( val incomingFlow: Flow = ws.incoming.consumeAsFlow() .mapNotNull { it as? Frame.Text } .map { it.readText() } - .map { Notification.fromString(it) } + .map { + Notification.fromString(it) + .apply { getValidation().validate(this).badRequestIfNotValid() } + } return build(ws.call.citizen, incomingFlow) { ws.outgoing.send(Text(it.toString()))