Skip to content

Commit

Permalink
Support regex format validation (#115)
Browse files Browse the repository at this point in the history
Resolves #54
  • Loading branch information
OptimumCode authored May 12, 2024
1 parent 22d3c8f commit 683a42f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Run reviewdog
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: reviewdog -reporter=github-pr-review
run: reviewdog -tee -reporter=github-pr-review
check-pr:
uses: ./.github/workflows/build-and-test.yml
with:
Expand Down
4 changes: 2 additions & 2 deletions .reviewdog.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
runner:
detekt:
cmd: "./gradlew -q detekt detektAll --console plain"
cmd: "./gradlew -q detekt detektAll --console plain 2>&1"
errorformat: # (optional if you use `format`)
- "%f:%l:%c: %m"
name: detekt
level: error
ktlint:
cmd: "./gradlew ktlintCheck --console plain"
cmd: "./gradlew ktlintCheck --console plain 2>&1"
errorformat: # (optional if you use `format`)
- "%f:%l:%c %m"
name: ktlint
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,18 @@ val valid = schema.validate(elementToValidate, errors::add)
## Format assertion

The library supports `format` assertion.
Almost all formats from [JSON schema draft 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3) are supported.
Unsupported formats:
* regex
All formats from [JSON schema draft 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3) are supported.

But there is an API to implement the user's defined format validation.
**_Implementation details:_**

+ **regex** - to implement regex format Kotlin `Regex` class is used.
Because of that, result might vary depending on the platform where validation is executed
([KT-49557](https://youtrack.jetbrains.com/issue/KT-49557)).
Please, be aware of it when using this library.</p>
If you know a KMM library that provides support for ECMA-262 Regex format
I would appreciate it if you could find some time to create [an issue](https://github.com/OptimumCode/json-schema-validator/issues/new/choose) with information about that library.

There is also an API to implement the user's defined format validation.
The [FormatValidator](src/commonMain/kotlin/io/github/optimumcode/json/schema/ValidationError.kt) interface can be user for that.
The custom format validators can be register in [JsonSchemaLoader](src/commonMain/kotlin/io/github/optimumcode/json/schema/JsonSchemaLoader.kt).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.github.optimumcode.json.schema.internal.formats.IpV6FormatValidator
import io.github.optimumcode.json.schema.internal.formats.IriFormatValidator
import io.github.optimumcode.json.schema.internal.formats.IriReferenceFormatValidator
import io.github.optimumcode.json.schema.internal.formats.JsonPointerFormatValidator
import io.github.optimumcode.json.schema.internal.formats.RegexFormatValidator
import io.github.optimumcode.json.schema.internal.formats.RelativeJsonPointerFormatValidator
import io.github.optimumcode.json.schema.internal.formats.TimeFormatValidator
import io.github.optimumcode.json.schema.internal.formats.UriFormatValidator
Expand Down Expand Up @@ -86,6 +87,7 @@ internal sealed class FormatAssertionFactory(
"uri-template" to UriTemplateFormatValidator,
"email" to EmailFormatValidator,
"idn-email" to IdnEmailFormatValidator,
"regex" to RegexFormatValidator,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.optimumcode.json.schema.internal.formats

import io.github.optimumcode.json.schema.FormatValidationResult
import io.github.optimumcode.json.schema.FormatValidator

/**
* [RegexFormatValidator] might not follow [ECMA-262](https://262.ecma-international.org/5.1/#sec-15.10) rules.
* This will depend on the underlying platform.
* Maybe one day there will be ECMA-262 regex library for KMM
*/
internal object RegexFormatValidator : AbstractStringFormatValidator() {
override fun validate(value: String): FormatValidationResult {
if (value.isEmpty()) {
return FormatValidator.Valid()
}
return try {
Regex(value)
FormatValidator.Valid()
} catch (_: Throwable) {
// throwable is handled because JS exception when regex is compiled does not extend Exception
FormatValidator.Invalid()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.optimumcode.json.schema.assertions.general.format

import io.kotest.core.spec.style.FunSpec

class JsonSchemaRegexFormatValidationTest : FunSpec() {
init {
formatValidationTestSuite(
format = "regex",
validTestCases =
listOf(
"",
"(?=test\\s)",
),
invalidTestCases =
listOf(
TestCase("(test", "missing brackets"),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ internal class TestFilter(
val excludeTests: Map<String, Set<String>> = emptyMap(),
)

internal val COMMON_FORMAT_FILTER =
TestFilter(
excludeSuites =
mapOf(
"regex" to emptySet(),
),
)
internal val COMMON_FORMAT_FILTER = TestFilter()

/**
* This class is a base for creating a test suite run from https://github.com/json-schema-org/JSON-Schema-Test-Suite.
Expand Down

0 comments on commit 683a42f

Please sign in to comment.