Skip to content

Commit 05fab61

Browse files
committed
Add failFast flag in flag output to skip remaining validations after first failure (#137)
Resolves #136 (cherry picked from commit 75b4fb4)
1 parent 28602ee commit 05fab61

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public sealed class OutputCollector<T> private constructor(
9393

9494
internal abstract fun onError(error: ValidationError)
9595

96+
internal open val isFailFast: Boolean
97+
get() = false
98+
9699
/**
97100
* A utility method that allows to call [reportErrors] method after the [block] has been executed
98101
*/
@@ -117,6 +120,9 @@ public sealed class OutputCollector<T> private constructor(
117120
override val output: Nothing
118121
get() = throw UnsupportedOperationException("no output in empty collector")
119122

123+
override val isFailFast: Boolean
124+
get() = true
125+
120126
override fun updateLocation(path: JsonPointer): OutputCollector<Nothing> = this
121127

122128
override fun updateKeywordLocation(
@@ -193,6 +199,8 @@ public sealed class OutputCollector<T> private constructor(
193199
) : OutputCollector<ValidationOutput.Flag>(parent, transformer) {
194200
private var valid: Boolean = true
195201
private var hasErrors: Boolean = false
202+
override val isFailFast: Boolean
203+
get() = true
196204
override val output: ValidationOutput.Flag
197205
get() =
198206
if (valid) {

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

+4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ internal class JsonSchemaRoot(
2424
var result = true
2525
context.pushSchemaPath(schemaPath, scopeId)
2626
errorCollector.updateKeywordLocation(schemaPath).use {
27+
val failFast = isFailFast
2728
assertions.forEach {
2829
val valid = it.validate(element, context, this)
2930
result = result and valid
31+
if (!result && failFast) {
32+
return@use
33+
}
3034
}
3135
}
3236
context.popSchemaPath()

0 commit comments

Comments
 (0)