Skip to content

Add module to wrap kotlin objects for validation #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ jobs:
gradle-version: wrapper
- name: Build and publish release
run: >
./gradlew --no-daemon --info :json-schema-validator:assemble
./gradlew --no-daemon --info
:json-schema-validator:assemble
:json-schema-validator-objects:assemble
publish closeAndReleaseStagingRepositories
-Pversion=${{ needs.version.outputs.RELEASE_VERSION }}
-x :benchmark:benchmark
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/snapshot_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- 'build.gradle.kts'
- 'gradle.properties'
- 'json-schema-validator/**'
- 'json-schema-validator-objects/**'
- 'gradle/**'
- 'generator/**'
- '.github/workflows/snapshot_release.yml'
Expand Down Expand Up @@ -54,6 +55,7 @@ jobs:
--no-daemon
--info
:json-schema-validator:assemble
:json-schema-validator-objects:assemble
publish
-x :benchmark:benchmark
env:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kotlin.code.style=official
kotlin.js.compiler=ir
org.gradle.jvmargs=-Xmx1G
org.gradle.jvmargs=-Xmx1536M
org.gradle.java.installations.auto-download=false
org.gradle.daemon=false

Expand Down
19 changes: 19 additions & 0 deletions json-schema-validator-objects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# JSON schema object wrapper

This module allows wrapping Kotlin objects (e.g. Map, List, primitives) to `AbstractElement` that can be validated by JSON schema.

## Usage

```kotlin
val schema = JsonSchema.fromDefinition(/*schema*/)

val obj = mapOf(
"a" to 42,
"b" to listOf("test"),
"c" to mapOf(
"inner" to 42,
),
)

val result = schema.validate(wrapAsElement(obj), OutputCollector.flag())
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public final class io/github/optimumcode/json/schema/wrappers/objects/ObjectWrappers {
public static final fun wrapAsElement (Ljava/lang/Object;)Lio/github/optimumcode/json/schema/model/AbstractElement;
public static final fun wrapAsElement (Ljava/lang/Object;Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;)Lio/github/optimumcode/json/schema/model/AbstractElement;
public static synthetic fun wrapAsElement$default (Ljava/lang/Object;Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;ILjava/lang/Object;)Lio/github/optimumcode/json/schema/model/AbstractElement;
}

public final class io/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration {
public static final field Companion Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration$Companion;
public static final fun create ()Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;
public static final fun create (Lkotlin/jvm/functions/Function1;)Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;
public final fun getAllowSets ()Z
public final fun getByteArrayAsBase64String ()Z
public final fun getCharAsCodepoint ()Z
}

public final class io/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration$Companion {
public final fun create ()Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;
public final fun create (Lkotlin/jvm/functions/Function1;)Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;
public static synthetic fun create$default (Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration$Companion;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/github/optimumcode/json/schema/wrappers/objects/WrappingConfiguration;
}

public final class io/github/optimumcode/json/schema/wrappers/objects/WrappingConfigurationBuilder {
public final fun getAllowSets ()Z
public final fun getByteArrayAsBase64String ()Z
public final fun getCharAsCodepoint ()Z
public final fun setAllowSets (Z)V
public final fun setByteArrayAsBase64String (Z)V
public final fun setCharAsCodepoint (Z)V
}

149 changes: 149 additions & 0 deletions json-schema-validator-objects/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
@file:OptIn(ExperimentalWasmDsl::class)

import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetWithTests
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType

plugins {
alias(libs.plugins.kotlin.mutliplatform)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotest.multiplatform)
alias(libs.plugins.kover)
alias(libs.plugins.detekt)
alias(libs.plugins.ktlint)
convention.publication
}

kotlin {
explicitApi()

@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
freeCompilerArgs.add("-opt-in=io.github.optimumcode.json.schema.ExperimentalApi")
}
jvmToolchain(11)
jvm {
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js(IR) {
browser()
generateTypeScriptDefinitions()
nodejs()
}
wasmJs {
// The wasmJsBrowserTest prints all executed tests as one unformatted string
// Have not found a way to suppress printing all this into console
browser()
nodejs()
}

applyDefaultHierarchyTemplate()

val macOsTargets =
listOf<KotlinTarget>(
macosX64(),
macosArm64(),
iosX64(),
iosArm64(),
iosSimulatorArm64(),
)

val linuxTargets =
listOf<KotlinTarget>(
linuxX64(),
linuxArm64(),
)

val windowsTargets =
listOf<KotlinTarget>(
mingwX64(),
)

sourceSets {
commonMain {
dependencies {
api(projects.jsonSchemaValidator)
}
}

val noJsMain by creating {
dependsOn(commonMain.get())
}

jvmMain {
dependsOn(noJsMain)
}

wasmJsMain {
dependsOn(noJsMain)
}

nativeMain {
dependsOn(noJsMain)
}

commonTest {
dependencies {
implementation(libs.kotest.assertions.core)
implementation(libs.kotest.framework.engine)
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
jvmTest {
dependencies {
implementation(libs.kotest.runner.junit5)
}
}
}

afterEvaluate {
fun Task.dependsOnTargetTests(targets: List<KotlinTarget>) {
targets.forEach {
if (it is KotlinTargetWithTests<*, *>) {
dependsOn(tasks.getByName("${it.name}Test"))
}
}
}
tasks.register("macOsAllTest") {
group = "verification"
description = "runs all tests for MacOS and IOS targets"
dependsOnTargetTests(macOsTargets)
}
tasks.register("windowsAllTest") {
group = "verification"
description = "runs all tests for Windows targets"
dependsOnTargetTests(windowsTargets)
}
tasks.register("linuxAllTest") {
group = "verification"
description = "runs all tests for Linux targets"
dependsOnTargetTests(linuxTargets)
dependsOn(tasks.getByName("jvmTest"))
dependsOn(tasks.getByName("jsTest"))
dependsOn(tasks.getByName("wasmJsTest"))
}
}
}

ktlint {
version.set(libs.versions.ktlint)
reporters {
reporter(ReporterType.HTML)
}
}

afterEvaluate {
val detektAllTask by tasks.register("detektAll") {
dependsOn(tasks.withType<Detekt>())
}

tasks.named("check").configure {
dependsOn(detektAllTask)
}
}
Loading
Loading