-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support regex format validation (#115)
Resolves #54
- Loading branch information
1 parent
22d3c8f
commit 683a42f
Showing
7 changed files
with
61 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...monMain/kotlin/io/github/optimumcode/json/schema/internal/formats/RegexFormatValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../optimumcode/json/schema/assertions/general/format/JsonSchemaRegexFormatValidationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters