Skip to content

Commit

Permalink
Add failFast flag in flag output to skip remaining validations afte…
Browse files Browse the repository at this point in the history
…r first failure (#137)

Resolves #136
  • Loading branch information
OptimumCode authored Jun 15, 2024
1 parent 869586b commit 75b4fb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public sealed class OutputCollector<T> private constructor(

internal abstract fun onError(error: ValidationError)

internal open val isFailFast: Boolean
get() = false

/**
* A utility method that allows to call [reportErrors] method after the [block] has been executed
*/
Expand All @@ -117,6 +120,9 @@ public sealed class OutputCollector<T> private constructor(
override val output: Nothing
get() = throw UnsupportedOperationException("no output in empty collector")

override val isFailFast: Boolean
get() = true

override fun updateLocation(path: JsonPointer): OutputCollector<Nothing> = this

override fun updateKeywordLocation(
Expand Down Expand Up @@ -193,6 +199,8 @@ public sealed class OutputCollector<T> private constructor(
) : OutputCollector<ValidationOutput.Flag>(parent, transformer) {
private var valid: Boolean = true
private var hasErrors: Boolean = false
override val isFailFast: Boolean
get() = true
override val output: ValidationOutput.Flag
get() =
if (valid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ internal class JsonSchemaRoot(
var result = true
context.pushSchemaPath(schemaPath, scopeId)
errorCollector.updateKeywordLocation(schemaPath).use {
val failFast = isFailFast
assertions.forEach {
val valid = it.validate(element, context, this)
result = result and valid
if (!result && failFast) {
return@use
}
}
}
context.popSchemaPath()
Expand Down

0 comments on commit 75b4fb4

Please sign in to comment.