Skip to content

Add uuid fromat validator #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ The library supports `format` assertion. For now only a few formats are supporte
* relative-json-pointer
* ipv4
* ipv6
* uuid

But there is 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.github.optimumcode.json.schema.internal.formats.IpV6FormatValidator
import io.github.optimumcode.json.schema.internal.formats.JsonPointerFormatValidator
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.UuidFormatValidator
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive

Expand Down Expand Up @@ -66,6 +67,7 @@ internal sealed class FormatAssertionFactory(
"relative-json-pointer" to RelativeJsonPointerFormatValidator,
"ipv4" to IpV4FormatValidator,
"ipv6" to IpV6FormatValidator,
"uuid" to UuidFormatValidator,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.optimumcode.json.schema.internal.formats

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

internal object UuidFormatValidator : AbstractStringFormatValidator() {
private const val UUID_LENGTH = 36
private const val HEX = "[0-9a-fA-F]"
private val UUID_REGEX = Regex("$HEX{8}-$HEX{4}-$HEX{4}-$HEX{4}-$HEX{12}")

override fun validate(value: String): FormatValidationResult {
if (value.isEmpty() || value.length != UUID_LENGTH) {
return FormatValidator.Invalid()
}
return if (UUID_REGEX.matches(value)) {
FormatValidator.Valid()
} else {
FormatValidator.Invalid()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.optimumcode.json.schema.assertions.general.format

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

class JsonSchemaUuidFormatValidationTest : FunSpec() {
init {
formatValidationTestSuite(
format = "uuid",
validTestCases =
listOf(
"f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6",
),
invalidTestCases =
listOf(
TestCase("", "empty value"),
TestCase("f81d4fae-7dec-11d0-a765-00a0c91e6bf", "too short"),
TestCase("f81d4fae-7dec-11d0-a765-00a0c91e6bf64", "too long"),
TestCase("f81d4fae-7dec-11d0-a7865-00a0c91e6bf64", "wrong block length"),
TestCase("f81d4fae-7dec-11d0-g765-00a0c91e6bf6", "invalid character"),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ internal val COMMON_FORMAT_FILTER =
"uri" to emptySet(),
"uri-reference" to emptySet(),
"uri-template" to emptySet(),
"uuid" to emptySet(),
),
)

Expand Down