-
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.
Add support for json-pointer and relative-json-pointer formats (#73)
Related to #54
- Loading branch information
1 parent
9a78967
commit b53d8fd
Showing
10 changed files
with
168 additions
and
4 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
29 changes: 29 additions & 0 deletions
29
...n/kotlin/io/github/optimumcode/json/schema/internal/formats/JsonPointerFormatValidator.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,29 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.pointer.JsonPointer | ||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
import io.github.optimumcode.json.schema.FormatValidator | ||
|
||
internal object JsonPointerFormatValidator : AbstractStringFormatValidator() { | ||
override fun validate(value: String): FormatValidationResult { | ||
if (value.isEmpty()) { | ||
return FormatValidator.Valid() | ||
} | ||
if (!value.startsWith(JsonPointer.SEPARATOR)) { | ||
return FormatValidator.Invalid() | ||
} | ||
var escape = false | ||
for (symbol in value) { | ||
if (escape && symbol != JsonPointer.QUOTATION_ESCAPE && symbol != JsonPointer.SEPARATOR_ESCAPE) { | ||
return FormatValidator.Invalid() | ||
} | ||
escape = symbol == JsonPointer.QUOTATION | ||
} | ||
return if (escape) { | ||
// escape character '~' in the end of the segment | ||
FormatValidator.Invalid() | ||
} else { | ||
FormatValidator.Valid() | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../io/github/optimumcode/json/schema/internal/formats/RelativeJsonPointerFormatValidator.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,49 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
import io.github.optimumcode.json.schema.FormatValidator | ||
|
||
internal object RelativeJsonPointerFormatValidator : AbstractStringFormatValidator() { | ||
private const val ZERO_CODE: Int = '0'.code | ||
private const val NINE_CODE: Int = '9'.code | ||
private const val REF_SYMBOL = '#' | ||
|
||
override fun validate(value: String): FormatValidationResult { | ||
if (value.isEmpty()) { | ||
return FormatValidator.Invalid() | ||
} | ||
val isFirstZero = value[0].code == ZERO_CODE | ||
for ((index, symbol) in value.withIndex()) { | ||
val code = symbol.code | ||
val isDigit = code in ZERO_CODE..NINE_CODE | ||
val isRef = symbol == REF_SYMBOL | ||
if (!isDigit) { | ||
return checkEnding(index, isRef, value) | ||
} | ||
if (code > ZERO_CODE && isFirstZero) { | ||
// leading zeros are not allowed | ||
return FormatValidator.Invalid() | ||
} | ||
} | ||
return FormatValidator.Valid() | ||
} | ||
|
||
private fun checkEnding( | ||
index: Int, | ||
isRef: Boolean, | ||
value: String, | ||
): FormatValidationResult = | ||
when { | ||
// we must have a digit at the beginning | ||
index == 0 -> FormatValidator.Invalid() | ||
isRef -> | ||
if (index == value.lastIndex) { | ||
FormatValidator.Valid() | ||
} else { | ||
// # must be the last character | ||
FormatValidator.Invalid() | ||
} | ||
|
||
else -> JsonPointerFormatValidator.validate(value.substring(index)) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...umcode/json/schema/assertions/general/format/JsonSchemaJsonPointerFormatValidationTest.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,27 @@ | ||
package io.github.optimumcode.json.schema.assertions.general.format | ||
|
||
import io.github.optimumcode.json.schema.assertions.general.format.FormatValidationTestSuite.TestCase | ||
import io.kotest.core.spec.style.FunSpec | ||
|
||
class JsonSchemaJsonPointerFormatValidationTest : FunSpec() { | ||
init { | ||
FormatValidationTestSuite( | ||
format = "json-pointer", | ||
validTestCases = | ||
listOf( | ||
"", | ||
"/", | ||
"/test//a", | ||
"/test/", | ||
"/tes~0", | ||
"/test~1", | ||
), | ||
invalidTestCases = | ||
listOf( | ||
TestCase("test", "does not start from separator"), | ||
TestCase("/test~2", "invalid quotation"), | ||
TestCase("/test~", "trailing quotation"), | ||
), | ||
).run { testFormat() } | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...son/schema/assertions/general/format/JsonSchemaRelativeJsonPointerFormatValidationTest.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,32 @@ | ||
package io.github.optimumcode.json.schema.assertions.general.format | ||
|
||
import io.github.optimumcode.json.schema.assertions.general.format.FormatValidationTestSuite.TestCase | ||
import io.kotest.core.spec.style.FunSpec | ||
|
||
class JsonSchemaRelativeJsonPointerFormatValidationTest : FunSpec() { | ||
init { | ||
FormatValidationTestSuite( | ||
format = "relative-json-pointer", | ||
validTestCases = | ||
listOf( | ||
"0", | ||
"1", | ||
"105", | ||
"0#", | ||
"105#", | ||
"0/test", | ||
"105/test", | ||
"0/0", | ||
), | ||
invalidTestCases = | ||
listOf( | ||
TestCase("", "empty RJP is not valid"), | ||
TestCase("01", "leading zeroes are not allowed"), | ||
TestCase("0##", "ref is the last character"), | ||
TestCase("0#/test", "ref and JSON pointer are not allowed"), | ||
TestCase("/test", "JSON pointer is not a valid RJP"), | ||
TestCase("test", "invalid Json Pointer"), | ||
), | ||
).run { testFormat() } | ||
} | ||
} |
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