Skip to content

Commit

Permalink
Add uuid fromat validator (#78)
Browse files Browse the repository at this point in the history
Related to #54
  • Loading branch information
OptimumCode authored Mar 8, 2024
1 parent 3657fff commit aa934d9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
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

0 comments on commit aa934d9

Please sign in to comment.