From aa21e989ee6d2773a29b9ebf44ef8ddad2974202 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Sat, 15 Jun 2024 16:23:20 +0400 Subject: [PATCH] Add failFast flag to skip remaning validations after first failure --- .../io/github/optimumcode/json/schema/OutputCollector.kt | 8 ++++++++ .../optimumcode/json/schema/internal/JsonSchemaRoot.kt | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt b/src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt index 3146291c..32daeb1b 100644 --- a/src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt +++ b/src/commonMain/kotlin/io/github/optimumcode/json/schema/OutputCollector.kt @@ -93,6 +93,9 @@ public sealed class OutputCollector 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 */ @@ -117,6 +120,9 @@ public sealed class OutputCollector 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 = this override fun updateKeywordLocation( @@ -193,6 +199,8 @@ public sealed class OutputCollector private constructor( ) : OutputCollector(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) { diff --git a/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/JsonSchemaRoot.kt b/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/JsonSchemaRoot.kt index ac472c2f..40779afb 100644 --- a/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/JsonSchemaRoot.kt +++ b/src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/JsonSchemaRoot.kt @@ -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()