Skip to content

Commit 1b02d70

Browse files
committed
Add documentation to new API
1 parent 6384383 commit 1b02d70

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

src/commonMain/kotlin/io/github/optimumcode/json/schema/AbsoluteLocation.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import kotlinx.serialization.descriptors.SerialDescriptor
1010
import kotlinx.serialization.encoding.Decoder
1111
import kotlinx.serialization.encoding.Encoder
1212

13+
/**
14+
* Class represents absolute location in JSON schema.
15+
* Serialized to a [String] like `https://schema.org#/path/in/schema`
16+
*/
1317
@Serializable(AbsoluteLocationSerializer::class)
1418
public data class AbsoluteLocation(
1519
val uri: Uri,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public class JsonSchema internal constructor(
3535
}
3636
}
3737

38+
/**
39+
* Validates [value] against this [JsonSchema].
40+
* The provided [outputCollector] will be used to collect the validation result.
41+
*
42+
* @return validation result depending on [outputCollector]
43+
*/
3844
public fun <T> validate(
3945
value: JsonElement,
4046
outputCollector: OutputCollector<T>,

src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ internal typealias OutputErrorTransformer<T> = OutputCollector<T>.(ValidationErr
1010

1111
private val NO_TRANSFORMATION: OutputErrorTransformer<*> = { it }
1212

13+
/**
14+
* Provides collectors' implementations defined in [draft 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#section-12.4)
15+
*/
1316
public sealed class OutputCollector<T> private constructor(
1417
parent: OutputCollector<T>? = null,
1518
transformer: OutputErrorTransformer<T> = NO_TRANSFORMATION,
@@ -46,22 +49,46 @@ public sealed class OutputCollector<T> private constructor(
4649
}
4750
} ?: transformer
4851

52+
/**
53+
* Sets current instance location to specified [path].
54+
* Returns an [OutputCollector] with updated location information.
55+
*/
4956
internal abstract fun updateLocation(path: JsonPointer): OutputCollector<T>
5057

58+
/**
59+
* Sets current keyword location to specified [path].
60+
* Updates absolute keyword location information to [absoluteLocation].
61+
* If [canCollapse] is `false` that will indicate the output node cannot be collapsed
62+
* (format might ignore this if it does not support collapsing).
63+
*/
5164
internal abstract fun updateKeywordLocation(
5265
path: JsonPointer,
5366
absoluteLocation: AbsoluteLocation? = null,
5467
canCollapse: Boolean = true,
5568
): OutputCollector<T>
5669

70+
/**
71+
* Add a transformation that should be applied to a reported [ValidationError].
72+
* The specified [transformer] will be combined with earlier specified transformations (if any were provided).
73+
* The transformation are applied in LIFO order.
74+
*/
5775
internal abstract fun withErrorTransformer(transformer: OutputErrorTransformer<T>): OutputCollector<T>
5876

77+
/**
78+
* Creates a child [OutputCollector] that has exactly same instance, keyword and absolute locations information.
79+
*/
5980
internal abstract fun childCollector(): OutputCollector<T>
6081

82+
/**
83+
* Commits the collected errors. Used to allow late error commit to support applicators like `oneOf`, `anyOf` etc.
84+
*/
6185
internal open fun reportErrors() = Unit
6286

6387
internal abstract fun onError(error: ValidationError)
6488

89+
/**
90+
* A utility method that allows to call [reportErrors] method after the [block] has been executed
91+
*/
6592
internal inline fun <OUT> use(block: OutputCollector<T>.() -> OUT): OUT =
6693
try {
6794
block(this)
@@ -71,6 +98,9 @@ public sealed class OutputCollector<T> private constructor(
7198

7299
protected fun transformError(error: ValidationError): ValidationError? = transformerFunc(error)
73100

101+
/**
102+
* Placeholder collector when no errors should be reported
103+
*/
74104
internal data object Empty : OutputCollector<Nothing>() {
75105
override val output: Nothing
76106
get() = throw UnsupportedOperationException("no output in empty collector")
@@ -90,6 +120,9 @@ public sealed class OutputCollector<T> private constructor(
90120
override fun onError(error: ValidationError) = Unit
91121
}
92122

123+
/**
124+
* Collector to pass all the collected errors to the provided [ErrorCollector]
125+
*/
93126
internal class DelegateOutputCollector(
94127
private val errorCollector: ErrorCollector,
95128
private val parent: DelegateOutputCollector? = null,

src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/JsonSchemaAssertion.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package io.github.optimumcode.json.schema.internal
22

3-
import io.github.optimumcode.json.schema.ErrorCollector
43
import io.github.optimumcode.json.schema.OutputCollector
54
import kotlinx.serialization.json.JsonElement
65

76
internal interface JsonSchemaAssertion {
87
/**
98
* Validates passes [element].
109
* If [element] does not pass the assertion returns `false`
11-
* and calls [ErrorCollector.onError] on passed [errorCollector].
12-
* Otherwise, returns `true`
10+
* and calls [OutputCollector.onError] on passed [errorCollector].
11+
* Otherwise, returns `true`.
12+
* Each [JsonSchemaAssertion] implementation MUST call [OutputCollector.updateKeywordLocation] to provide information
13+
* about assertion location.
14+
* If [JsonSchemaAssertion] implementation updates [AssertionContext.objectPath] it also must call
15+
* [OutputCollector.updateLocation] to update current instance location.
1316
*
1417
* @param element JSON element to validate
1518
* @param context context associated with the [element]

0 commit comments

Comments
 (0)