Skip to content

Commit 16802e4

Browse files
committed
Add vaildate methods to JsonSchema that accept abstract element
1 parent 118f6f8 commit 16802e4

File tree

1 file changed

+30
-3
lines changed
  • json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema

1 file changed

+30
-3
lines changed

json-schema-validator/src/commonMain/kotlin/io/github/optimumcode/json/schema/JsonSchema.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.optimumcode.json.schema.internal.DefaultReferenceResolver
77
import io.github.optimumcode.json.schema.internal.IsolatedLoader
88
import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion
99
import io.github.optimumcode.json.schema.internal.wrapper.wrap
10+
import io.github.optimumcode.json.schema.model.AbstractElement
1011
import kotlinx.serialization.json.JsonElement
1112
import kotlin.jvm.JvmOverloads
1213
import kotlin.jvm.JvmStatic
@@ -29,10 +30,35 @@ public class JsonSchema internal constructor(
2930
public fun validate(
3031
value: JsonElement,
3132
errorCollector: ErrorCollector,
33+
): Boolean = validate(value.wrap(), errorCollector)
34+
35+
/**
36+
* Validates [value] against this [JsonSchema].
37+
* The provided [outputCollectorProvider] will be used to create [OutputCollector]
38+
* which collects the validation result.
39+
*
40+
* @return validation result depending on [outputCollectorProvider]
41+
*/
42+
public fun <T> validate(
43+
value: JsonElement,
44+
outputCollectorProvider: OutputCollector.Provider<T>,
45+
): T = validate(value.wrap(), outputCollectorProvider)
46+
47+
/**
48+
* Validates [value] against this [JsonSchema].
49+
* If the [value] is valid against the schema the function returns `true`.
50+
* Otherwise, it returns `false`.
51+
*
52+
* All reported errors will be reported to [ErrorCollector.onError]
53+
*/
54+
@ExperimentalApi
55+
public fun validate(
56+
value: AbstractElement,
57+
errorCollector: ErrorCollector,
3258
): Boolean {
3359
val context = DefaultAssertionContext(JsonPointer.ROOT, referenceResolver)
3460
return DelegateOutputCollector(errorCollector).use {
35-
assertion.validate(value.wrap(), context, this)
61+
assertion.validate(value, context, this)
3662
}
3763
}
3864

@@ -43,14 +69,15 @@ public class JsonSchema internal constructor(
4369
*
4470
* @return validation result depending on [outputCollectorProvider]
4571
*/
72+
@ExperimentalApi
4673
public fun <T> validate(
47-
value: JsonElement,
74+
value: AbstractElement,
4875
outputCollectorProvider: OutputCollector.Provider<T>,
4976
): T {
5077
val context = DefaultAssertionContext(JsonPointer.ROOT, referenceResolver)
5178
val collector = outputCollectorProvider.get()
5279
collector.use {
53-
assertion.validate(value.wrap(), context, this)
80+
assertion.validate(value, context, this)
5481
}
5582
return collector.output
5683
}

0 commit comments

Comments
 (0)