-
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 partial support for format keyword (#72)
This patch introduces: - API for implementing user's format validator and registering it in the schema - Support for data/time formats Related to #54
- Loading branch information
1 parent
b1bd666
commit 4f7cdbf
Showing
32 changed files
with
1,319 additions
and
77 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
13 changes: 13 additions & 0 deletions
13
src/commonMain/kotlin/io/github/optimumcode/json/schema/Annotations.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 @@ | ||
@file:JvmName("Annotations") | ||
|
||
package io.github.optimumcode.json.schema | ||
|
||
import io.github.optimumcode.json.schema.internal.factories.general.FormatAssertionFactory | ||
import kotlin.jvm.JvmField | ||
import kotlin.jvm.JvmName | ||
|
||
/** | ||
* Key for getting annotation from `format` assertion | ||
*/ | ||
@JvmField | ||
public val FORMAT_ANNOTATION: AnnotationKey<String> = FormatAssertionFactory.ANNOTATION |
11 changes: 11 additions & 0 deletions
11
src/commonMain/kotlin/io/github/optimumcode/json/schema/ExperimentalApi.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,11 @@ | ||
package io.github.optimumcode.json.schema | ||
|
||
/** | ||
* Marks declarations that are experimental or published as a 'preview' version. | ||
* The API for those declarations can be changed in future releases | ||
* based on library needs or user's feedback. | ||
* Once the API is final the backward compatibility will be maintained within patch and minor updates. | ||
*/ | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION, AnnotationTarget.TYPEALIAS) | ||
@RequiresOptIn(level = RequiresOptIn.Level.WARNING) | ||
public annotation class ExperimentalApi |
51 changes: 51 additions & 0 deletions
51
src/commonMain/kotlin/io/github/optimumcode/json/schema/FormatValidator.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,51 @@ | ||
package io.github.optimumcode.json.schema | ||
|
||
import kotlinx.serialization.json.JsonElement | ||
import kotlin.jvm.JvmStatic | ||
|
||
/** | ||
* The [FormatValidator] is used to check whether the [JsonElement] matches the expected format. | ||
* If the [JsonElement] is not of the required type (e.g. validator expects string but the [JsonElement] is an object) | ||
* the validator **MUST** return [FormatValidator.Valid] result | ||
*/ | ||
@ExperimentalApi | ||
public interface FormatValidator { | ||
/** | ||
* Validates [element] against the expected format | ||
* | ||
* @param element JSON element to validate against the expected format | ||
* @return the result of the validation | ||
*/ | ||
public fun validate(element: JsonElement): FormatValidationResult | ||
|
||
public companion object { | ||
@Suppress("ktlint:standard:function-naming", "FunctionName") | ||
@JvmStatic | ||
public fun Valid(): FormatValidationResult = FormatValidationResult.Valid | ||
|
||
@Suppress("ktlint:standard:function-naming", "FunctionName") | ||
@JvmStatic | ||
public fun Invalid(): FormatValidationResult = FormatValidationResult.Invalid | ||
} | ||
} | ||
|
||
@ExperimentalApi | ||
public sealed class FormatValidationResult private constructor(private val valid: Boolean) { | ||
public fun isValid(): Boolean = valid | ||
|
||
internal data object Valid : FormatValidationResult(true) | ||
|
||
internal data object Invalid : FormatValidationResult(false) | ||
} | ||
|
||
public enum class FormatBehavior { | ||
/** | ||
* Only annotation. If the value does not match format the validation will pass | ||
*/ | ||
ANNOTATION_ONLY, | ||
|
||
/** | ||
* Annotation and assertion. If the value does not match format the validation will fail | ||
*/ | ||
ANNOTATION_AND_ASSERTION, | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/commonMain/kotlin/io/github/optimumcode/json/schema/SchemaOption.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,11 @@ | ||
package io.github.optimumcode.json.schema | ||
|
||
import kotlin.jvm.JvmField | ||
import kotlin.reflect.KClass | ||
|
||
public class SchemaOption<T : Any> private constructor(internal val type: KClass<T>) { | ||
public companion object { | ||
@JvmField | ||
public val FORMAT_BEHAVIOR_OPTION: SchemaOption<FormatBehavior> = SchemaOption(FormatBehavior::class) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/LoadingContext.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
Oops, something went wrong.