Skip to content

Commit aa934d9

Browse files
authored
Add uuid fromat validator (#78)
Related to #54
1 parent 3657fff commit aa934d9

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ The library supports `format` assertion. For now only a few formats are supporte
297297
* relative-json-pointer
298298
* ipv4
299299
* ipv6
300+
* uuid
300301

301302
But there is an API to implement the user's defined format validation.
302303
The [FormatValidator](src/commonMain/kotlin/io/github/optimumcode/json/schema/ValidationError.kt) interface can be user for that.

src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/factories/general/FormatAssertionFactory.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.github.optimumcode.json.schema.internal.formats.IpV6FormatValidator
2121
import io.github.optimumcode.json.schema.internal.formats.JsonPointerFormatValidator
2222
import io.github.optimumcode.json.schema.internal.formats.RelativeJsonPointerFormatValidator
2323
import io.github.optimumcode.json.schema.internal.formats.TimeFormatValidator
24+
import io.github.optimumcode.json.schema.internal.formats.UuidFormatValidator
2425
import kotlinx.serialization.json.JsonElement
2526
import kotlinx.serialization.json.JsonPrimitive
2627

@@ -66,6 +67,7 @@ internal sealed class FormatAssertionFactory(
6667
"relative-json-pointer" to RelativeJsonPointerFormatValidator,
6768
"ipv4" to IpV4FormatValidator,
6869
"ipv6" to IpV6FormatValidator,
70+
"uuid" to UuidFormatValidator,
6971
)
7072
}
7173
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.optimumcode.json.schema.internal.formats
2+
3+
import io.github.optimumcode.json.schema.FormatValidationResult
4+
import io.github.optimumcode.json.schema.FormatValidator
5+
6+
internal object UuidFormatValidator : AbstractStringFormatValidator() {
7+
private const val UUID_LENGTH = 36
8+
private const val HEX = "[0-9a-fA-F]"
9+
private val UUID_REGEX = Regex("$HEX{8}-$HEX{4}-$HEX{4}-$HEX{4}-$HEX{12}")
10+
11+
override fun validate(value: String): FormatValidationResult {
12+
if (value.isEmpty() || value.length != UUID_LENGTH) {
13+
return FormatValidator.Invalid()
14+
}
15+
return if (UUID_REGEX.matches(value)) {
16+
FormatValidator.Valid()
17+
} else {
18+
FormatValidator.Invalid()
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.optimumcode.json.schema.assertions.general.format
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
5+
class JsonSchemaUuidFormatValidationTest : FunSpec() {
6+
init {
7+
formatValidationTestSuite(
8+
format = "uuid",
9+
validTestCases =
10+
listOf(
11+
"f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
12+
"F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6",
13+
),
14+
invalidTestCases =
15+
listOf(
16+
TestCase("", "empty value"),
17+
TestCase("f81d4fae-7dec-11d0-a765-00a0c91e6bf", "too short"),
18+
TestCase("f81d4fae-7dec-11d0-a765-00a0c91e6bf64", "too long"),
19+
TestCase("f81d4fae-7dec-11d0-a7865-00a0c91e6bf64", "wrong block length"),
20+
TestCase("f81d4fae-7dec-11d0-g765-00a0c91e6bf6", "invalid character"),
21+
),
22+
)
23+
}
24+
}

test-suites/src/commonTest/kotlin/io/github/optimumcode/json/schema/suite/AbstractSchemaTestSuite.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ internal val COMMON_FORMAT_FILTER =
5656
"uri" to emptySet(),
5757
"uri-reference" to emptySet(),
5858
"uri-template" to emptySet(),
59-
"uuid" to emptySet(),
6059
),
6160
)
6261

0 commit comments

Comments
 (0)