Skip to content

Commit 683a42f

Browse files
authored
Support regex format validation (#115)
Resolves #54
1 parent 22d3c8f commit 683a42f

File tree

7 files changed

+61
-14
lines changed

7 files changed

+61
-14
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Run reviewdog
5050
env:
5151
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52-
run: reviewdog -reporter=github-pr-review
52+
run: reviewdog -tee -reporter=github-pr-review
5353
check-pr:
5454
uses: ./.github/workflows/build-and-test.yml
5555
with:

.reviewdog.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
runner:
22
detekt:
3-
cmd: "./gradlew -q detekt detektAll --console plain"
3+
cmd: "./gradlew -q detekt detektAll --console plain 2>&1"
44
errorformat: # (optional if you use `format`)
55
- "%f:%l:%c: %m"
66
name: detekt
77
level: error
88
ktlint:
9-
cmd: "./gradlew ktlintCheck --console plain"
9+
cmd: "./gradlew ktlintCheck --console plain 2>&1"
1010
errorformat: # (optional if you use `format`)
1111
- "%f:%l:%c %m"
1212
name: ktlint

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,18 @@ val valid = schema.validate(elementToValidate, errors::add)
333333
## Format assertion
334334

335335
The library supports `format` assertion.
336-
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.
337-
Unsupported formats:
338-
* regex
336+
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.
339337

340-
But there is an API to implement the user's defined format validation.
338+
**_Implementation details:_**
339+
340+
+ **regex** - to implement regex format Kotlin `Regex` class is used.
341+
Because of that, result might vary depending on the platform where validation is executed
342+
([KT-49557](https://youtrack.jetbrains.com/issue/KT-49557)).
343+
Please, be aware of it when using this library.</p>
344+
If you know a KMM library that provides support for ECMA-262 Regex format
345+
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.
346+
347+
There is also an API to implement the user's defined format validation.
341348
The [FormatValidator](src/commonMain/kotlin/io/github/optimumcode/json/schema/ValidationError.kt) interface can be user for that.
342349
The custom format validators can be register in [JsonSchemaLoader](src/commonMain/kotlin/io/github/optimumcode/json/schema/JsonSchemaLoader.kt).
343350

src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/general/FormatAssertionFactory.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.github.optimumcode.json.schema.internal.formats.IpV6FormatValidator
2525
import io.github.optimumcode.json.schema.internal.formats.IriFormatValidator
2626
import io.github.optimumcode.json.schema.internal.formats.IriReferenceFormatValidator
2727
import io.github.optimumcode.json.schema.internal.formats.JsonPointerFormatValidator
28+
import io.github.optimumcode.json.schema.internal.formats.RegexFormatValidator
2829
import io.github.optimumcode.json.schema.internal.formats.RelativeJsonPointerFormatValidator
2930
import io.github.optimumcode.json.schema.internal.formats.TimeFormatValidator
3031
import io.github.optimumcode.json.schema.internal.formats.UriFormatValidator
@@ -86,6 +87,7 @@ internal sealed class FormatAssertionFactory(
8687
"uri-template" to UriTemplateFormatValidator,
8788
"email" to EmailFormatValidator,
8889
"idn-email" to IdnEmailFormatValidator,
90+
"regex" to RegexFormatValidator,
8991
)
9092
}
9193
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.optimumcode.json.schema.internal.formats
2+
3+
import io.github.optimumcode.json.schema.FormatValidationResult
4+
import io.github.optimumcode.json.schema.FormatValidator
5+
6+
/**
7+
* [RegexFormatValidator] might not follow [ECMA-262](https://262.ecma-international.org/5.1/#sec-15.10) rules.
8+
* This will depend on the underlying platform.
9+
* Maybe one day there will be ECMA-262 regex library for KMM
10+
*/
11+
internal object RegexFormatValidator : AbstractStringFormatValidator() {
12+
override fun validate(value: String): FormatValidationResult {
13+
if (value.isEmpty()) {
14+
return FormatValidator.Valid()
15+
}
16+
return try {
17+
Regex(value)
18+
FormatValidator.Valid()
19+
} catch (_: Throwable) {
20+
// throwable is handled because JS exception when regex is compiled does not extend Exception
21+
FormatValidator.Invalid()
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.optimumcode.json.schema.assertions.general.format
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
5+
class JsonSchemaRegexFormatValidationTest : FunSpec() {
6+
init {
7+
formatValidationTestSuite(
8+
format = "regex",
9+
validTestCases =
10+
listOf(
11+
"",
12+
"(?=test\\s)",
13+
),
14+
invalidTestCases =
15+
listOf(
16+
TestCase("(test", "missing brackets"),
17+
),
18+
)
19+
}
20+
}

test-suites/src/commonTest/kotlin/io/github/optimumcode/json/schema/suite/AbstractSchemaTestSuite.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ internal class TestFilter(
4343
val excludeTests: Map<String, Set<String>> = emptyMap(),
4444
)
4545

46-
internal val COMMON_FORMAT_FILTER =
47-
TestFilter(
48-
excludeSuites =
49-
mapOf(
50-
"regex" to emptySet(),
51-
),
52-
)
46+
internal val COMMON_FORMAT_FILTER = TestFilter()
5347

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

0 commit comments

Comments
 (0)