-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,163 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
val coroutinesVersion: String by project | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
|
||
testImplementation(kotlin("test")) | ||
testImplementation(kotlin("test-junit")) | ||
|
||
testImplementation("io.konform:konform:0.4.0") | ||
testImplementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") | ||
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") | ||
} |
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,61 @@ | ||
package ru.otus.otuskotlin.marketplace.lib.koform | ||
|
||
import io.konform.validation.Invalid | ||
import io.konform.validation.Validation | ||
import io.konform.validation.ValidationBuilder | ||
import io.konform.validation.jsonschema.pattern | ||
import kotlinx.datetime.* | ||
import org.junit.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertIs | ||
import kotlin.time.Duration.Companion.days | ||
|
||
// Konform - DSL для валидации | ||
class KonformTest { | ||
@Test | ||
fun konform() { | ||
val objValidator = Validation<SomeObject> { | ||
SomeObject::userId { | ||
pattern("^[0-9a-zA-Z_-]{1,64}\$") | ||
} | ||
SomeObject::dob { | ||
minAge(15) | ||
} | ||
} | ||
|
||
val resultUserId = objValidator.validate(SomeObject()) | ||
assertIs<Invalid<SomeObject>>(resultUserId) | ||
val errorUserId = resultUserId.errors.firstOrNull { it.dataPath == ".userId" } | ||
assertContains(errorUserId?.message ?: "", "pattern") | ||
println("RESULT: ${errorUserId?.dataPath} ${errorUserId?.message}") | ||
|
||
val resultAge = objValidator.validate(SomeObject(userId = "987987987")) | ||
assertIs<Invalid<SomeObject>>(resultAge) | ||
val errorAge = resultAge.errors.firstOrNull { it.dataPath == ".dob" } | ||
assertContains(errorAge?.message ?: "", "Age") | ||
println("RESULT: ${errorAge?.dataPath} ${errorAge?.message}") | ||
} | ||
} | ||
|
||
private fun ValidationBuilder<LocalDate?>.minAge(age: Int) = addConstraint( | ||
errorMessage = "Age cannot be in range of 15..130 years", | ||
"15..130" | ||
) { | ||
if (it == null) { | ||
println("EMPTY!") | ||
false | ||
} else { | ||
val date = Clock.System | ||
.now() | ||
.minus((age * 365).days) | ||
.toLocalDateTime(TimeZone.currentSystemDefault()) | ||
.date | ||
println("DATE: $date ~ $it ${it.compareTo(date)}") | ||
(it.compareTo(date) in 0..1) | ||
} | ||
} | ||
|
||
data class SomeObject( | ||
val userId: String = "", | ||
val dob: LocalDate? = null, | ||
) |
65 changes: 65 additions & 0 deletions
65
...k-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2FromTransportValidated.kt
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,65 @@ | ||
package ru.otus.otuskotlin.marketplace.api.v2.mappers | ||
|
||
import ru.otus.otuskotlin.marketplace.api.v2.models.* | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.models.* | ||
import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs | ||
|
||
// Демонстрация форматной валидации в мапере | ||
private sealed interface Result<T,E> | ||
private data class Ok<T,E>(val value: T) : Result<T,E> | ||
private data class Err<T,E>(val errors: List<E>) : Result<T,E> { | ||
constructor(error: E) : this(listOf(error)) | ||
} | ||
|
||
private fun <T,E> Result<T,E>.getOrExec(default: T, block: (Err<T,E>) -> Unit = {}): T = when (this) { | ||
is Ok<T,E> -> this.value | ||
is Err<T,E> -> { | ||
block(this) | ||
default | ||
} | ||
} | ||
|
||
@Suppress("unused") | ||
private fun <T,E> Result<T,E>.getOrNull(block: (Err<T,E>) -> Unit = {}): T? = when (this) { | ||
is Ok<T,E> -> this.value | ||
is Err<T,E> -> { | ||
block(this) | ||
null | ||
} | ||
} | ||
|
||
private fun String?.transportToStubCaseValidated(): Result<MkplStubs,MkplError> = when (this) { | ||
"success" -> Ok(MkplStubs.SUCCESS) | ||
"notFound" -> Ok(MkplStubs.NOT_FOUND) | ||
"badId" -> Ok(MkplStubs.BAD_ID) | ||
"badTitle" -> Ok(MkplStubs.BAD_TITLE) | ||
"badDescription" -> Ok(MkplStubs.BAD_DESCRIPTION) | ||
"badVisibility" -> Ok(MkplStubs.BAD_VISIBILITY) | ||
"cannotDelete" -> Ok(MkplStubs.CANNOT_DELETE) | ||
"badSearchString" -> Ok(MkplStubs.BAD_SEARCH_STRING) | ||
null -> Ok(MkplStubs.NONE) | ||
else -> Err( | ||
MkplError( | ||
code = "wrong-stub-case", | ||
group = "mapper-validation", | ||
field = "debug.stub", | ||
message = "Unsupported value for case \"$this\"" | ||
) | ||
) | ||
} | ||
|
||
@Suppress("unused") | ||
fun MkplContext.fromTransportValidated(request: AdCreateRequest) { | ||
command = MkplCommand.CREATE | ||
// Вся магия здесь! | ||
stubCase = request | ||
.debug | ||
?.stub | ||
?.value | ||
.transportToStubCaseValidated() | ||
.getOrExec(MkplStubs.NONE) { err: Err<MkplStubs,MkplError> -> | ||
errors.addAll(err.errors) | ||
state = MkplState.FAILING | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...etplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperValidatedTest.kt
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,25 @@ | ||
package ru.otus.otuskotlin.marketplace.api.v2.mappers | ||
|
||
import ru.otus.otuskotlin.marketplace.api.v2.models.AdCreateRequest | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.AdDebug | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.AdRequestDebugStubs | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class MapperValidatedTest { | ||
@Test | ||
fun fromTransportValidated() { | ||
val req = AdCreateRequest( | ||
debug = AdDebug( | ||
stub = AdRequestDebugStubs.SUCCESS, | ||
), | ||
) | ||
|
||
val context = MkplContext() | ||
context.fromTransportValidated(req) | ||
|
||
assertEquals(MkplStubs.SUCCESS, context.stubCase) | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/validation/FinishValidation.kt
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,22 @@ | ||
package ru.otus.otuskotlin.marketplace.biz.validation | ||
|
||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplState | ||
import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl | ||
import ru.otus.otuskotlin.marketplace.cor.worker | ||
|
||
fun ICorChainDsl<MkplContext>.finishAdValidation(title: String) = worker { | ||
this.title = title | ||
on { state == MkplState.RUNNING } | ||
handle { | ||
adValidated = adValidating | ||
} | ||
} | ||
|
||
fun ICorChainDsl<MkplContext>.finishAdFilterValidation(title: String) = worker { | ||
this.title = title | ||
on { state == MkplState.RUNNING } | ||
handle { | ||
adFilterValidated = adFilterValidating | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...e-be/ok-marketplace-biz/src/commonMain/kotlin/validation/ValidateDescriptionHasContent.kt
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,23 @@ | ||
package ru.otus.otuskotlin.marketplace.biz.validation | ||
|
||
import ru.otus.otuskotlin.marketplace.common.helpers.errorValidation | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.helpers.fail | ||
import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl | ||
import ru.otus.otuskotlin.marketplace.cor.worker | ||
|
||
// пример обработки ошибки в рамках бизнес-цепочки | ||
fun ICorChainDsl<MkplContext>.validateDescriptionHasContent(title: String) = worker { | ||
this.title = title | ||
val regExp = Regex("\\p{L}") | ||
on { adValidating.description.isNotEmpty() && !adValidating.description.contains(regExp) } | ||
handle { | ||
fail( | ||
errorValidation( | ||
field = "description", | ||
violationCode = "noContent", | ||
description = "field must contain letters" | ||
) | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ace-be/ok-marketplace-biz/src/commonMain/kotlin/validation/ValidateDescriptionNotEmpty.kt
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,21 @@ | ||
package ru.otus.otuskotlin.marketplace.biz.validation | ||
|
||
import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl | ||
import ru.otus.otuskotlin.marketplace.cor.worker | ||
import ru.otus.otuskotlin.marketplace.common.helpers.errorValidation | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.helpers.fail | ||
|
||
fun ICorChainDsl<MkplContext>.validateDescriptionNotEmpty(title: String) = worker { | ||
this.title = title | ||
on { adValidating.description.isEmpty() } | ||
handle { | ||
fail( | ||
errorValidation( | ||
field = "description", | ||
violationCode = "empty", | ||
description = "field must not be empty" | ||
) | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/validation/ValidateIdNotEmpty.kt
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,21 @@ | ||
package ru.otus.otuskotlin.marketplace.biz.validation | ||
|
||
import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl | ||
import ru.otus.otuskotlin.marketplace.cor.worker | ||
import ru.otus.otuskotlin.marketplace.common.helpers.errorValidation | ||
import ru.otus.otuskotlin.marketplace.common.MkplContext | ||
import ru.otus.otuskotlin.marketplace.common.helpers.fail | ||
|
||
fun ICorChainDsl<MkplContext>.validateIdNotEmpty(title: String) = worker { | ||
this.title = title | ||
on { adValidating.id.asString().isEmpty() } | ||
handle { | ||
fail( | ||
errorValidation( | ||
field = "id", | ||
violationCode = "empty", | ||
description = "field must not be empty" | ||
) | ||
) | ||
} | ||
} |
Oops, something went wrong.