-
Notifications
You must be signed in to change notification settings - Fork 4
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 uri and iri formats (#99)
Related to #54
- Loading branch information
1 parent
e458073
commit cb90f5e
Showing
15 changed files
with
839 additions
and
7 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
13 changes: 13 additions & 0 deletions
13
...ommonMain/kotlin/io/github/optimumcode/json/schema/internal/formats/IriFormatValidator.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,13 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
|
||
internal object IriFormatValidator : AbstractStringFormatValidator() { | ||
override fun validate(value: String): FormatValidationResult { | ||
if (value.isEmpty()) { | ||
return UriFormatValidator.validate(value) | ||
} | ||
val uri = IriSpec.covertToUri(value) | ||
return UriFormatValidator.validate(uri) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../kotlin/io/github/optimumcode/json/schema/internal/formats/IriReferenceFormatValidator.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,13 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
|
||
internal object IriReferenceFormatValidator : AbstractStringFormatValidator() { | ||
override fun validate(value: String): FormatValidationResult { | ||
if (value.isEmpty()) { | ||
return UriReferenceFormatValidator.validate(value) | ||
} | ||
val uri = IriSpec.covertToUri(value) | ||
return UriReferenceFormatValidator.validate(uri) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/formats/IriSpec.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,22 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
internal object IriSpec { | ||
private const val BITS_SHIFT = 4 | ||
private const val LOWER_BITS = 0x0F | ||
private const val HEX_DECIMAL = "0123456789ABCDEF" | ||
|
||
fun covertToUri(iri: String): String { | ||
return buildString { | ||
for (byte in iri.encodeToByteArray()) { | ||
if (byte >= 0) { | ||
append(byte.toInt().toChar()) | ||
} else { | ||
val unsignedInt = byte.toUByte().toInt() | ||
append('%') | ||
append(HEX_DECIMAL[unsignedInt shr BITS_SHIFT]) | ||
append(HEX_DECIMAL[unsignedInt and LOWER_BITS]) | ||
} | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ommonMain/kotlin/io/github/optimumcode/json/schema/internal/formats/UriFormatValidator.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,65 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
import io.github.optimumcode.json.schema.FormatValidator | ||
import io.github.optimumcode.json.schema.internal.formats.UriSpec.FRAGMENT_DELIMITER | ||
import io.github.optimumcode.json.schema.internal.formats.UriSpec.QUERY_DELIMITER | ||
import io.github.optimumcode.json.schema.internal.formats.UriSpec.SCHEMA_DELIMITER | ||
|
||
internal object UriFormatValidator : AbstractStringFormatValidator() { | ||
@Suppress("detekt:ReturnCount") | ||
override fun validate(value: String): FormatValidationResult { | ||
if (value.isEmpty()) { | ||
return FormatValidator.Invalid() | ||
} | ||
|
||
val schemaEndIndex = value.indexOf(SCHEMA_DELIMITER) | ||
if (schemaEndIndex < 0 || schemaEndIndex == value.lastIndex) { | ||
return FormatValidator.Invalid() | ||
} | ||
|
||
val schema = value.substring(0, schemaEndIndex) | ||
if (!UriSpec.isValidSchema(schema)) { | ||
return FormatValidator.Invalid() | ||
} | ||
|
||
val fragmentDelimiterIndex = value.indexOf(FRAGMENT_DELIMITER) | ||
val queryDelimiterIndex = | ||
value.indexOf(QUERY_DELIMITER) | ||
.takeUnless { fragmentDelimiterIndex in 0..<it } | ||
?: -1 | ||
val hierPart = | ||
when { | ||
queryDelimiterIndex > 0 -> | ||
value.substring(schemaEndIndex + 1, queryDelimiterIndex) | ||
fragmentDelimiterIndex > 0 -> | ||
value.substring(schemaEndIndex + 1, fragmentDelimiterIndex) | ||
else -> | ||
value.substring(schemaEndIndex + 1) | ||
} | ||
if (!UriSpec.isValidHierPart(hierPart)) { | ||
return FormatValidator.Invalid() | ||
} | ||
|
||
if (queryDelimiterIndex > 0 && queryDelimiterIndex < value.lastIndex) { | ||
val query = | ||
if (fragmentDelimiterIndex > 0) { | ||
value.substring(queryDelimiterIndex + 1, fragmentDelimiterIndex) | ||
} else { | ||
value.substring(queryDelimiterIndex + 1) | ||
} | ||
if (!UriSpec.isValidQuery(query)) { | ||
return FormatValidator.Invalid() | ||
} | ||
} | ||
|
||
if (fragmentDelimiterIndex > 0 && fragmentDelimiterIndex < value.lastIndex) { | ||
val fragment = value.substring(fragmentDelimiterIndex + 1) | ||
if (!UriSpec.isValidFragment(fragment)) { | ||
return FormatValidator.Invalid() | ||
} | ||
} | ||
|
||
return FormatValidator.Valid() | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../kotlin/io/github/optimumcode/json/schema/internal/formats/UriReferenceFormatValidator.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,53 @@ | ||
package io.github.optimumcode.json.schema.internal.formats | ||
|
||
import io.github.optimumcode.json.schema.FormatValidationResult | ||
import io.github.optimumcode.json.schema.FormatValidator | ||
import io.github.optimumcode.json.schema.internal.formats.UriSpec.FRAGMENT_DELIMITER | ||
import io.github.optimumcode.json.schema.internal.formats.UriSpec.QUERY_DELIMITER | ||
|
||
internal object UriReferenceFormatValidator : AbstractStringFormatValidator() { | ||
@Suppress("detekt:ReturnCount") | ||
override fun validate(value: String): FormatValidationResult { | ||
if (UriFormatValidator.validate(value).isValid()) { | ||
return FormatValidator.Valid() | ||
} | ||
|
||
val fragmentDelimiterIndex = value.indexOf(FRAGMENT_DELIMITER) | ||
val queryDelimiterIndex = | ||
value.indexOf(QUERY_DELIMITER) | ||
.takeUnless { fragmentDelimiterIndex in 0..<it } | ||
?: -1 | ||
val relativePart = | ||
when { | ||
queryDelimiterIndex >= 0 -> | ||
value.substring(0, queryDelimiterIndex) | ||
fragmentDelimiterIndex >= 0 -> | ||
value.substring(0, fragmentDelimiterIndex) | ||
else -> value | ||
} | ||
if (!UriSpec.isValidRelativePart(relativePart)) { | ||
return FormatValidator.Invalid() | ||
} | ||
|
||
if (queryDelimiterIndex >= 0 && queryDelimiterIndex < value.lastIndex) { | ||
val query = | ||
if (fragmentDelimiterIndex > 0) { | ||
value.substring(queryDelimiterIndex + 1, fragmentDelimiterIndex) | ||
} else { | ||
value.substring(queryDelimiterIndex + 1) | ||
} | ||
if (!UriSpec.isValidQuery(query)) { | ||
return FormatValidator.Invalid() | ||
} | ||
} | ||
|
||
if (fragmentDelimiterIndex >= 0 && fragmentDelimiterIndex < value.lastIndex) { | ||
val fragment = value.substring(fragmentDelimiterIndex + 1) | ||
if (!UriSpec.isValidFragment(fragment)) { | ||
return FormatValidator.Invalid() | ||
} | ||
} | ||
|
||
return FormatValidator.Valid() | ||
} | ||
} |
Oops, something went wrong.