Skip to content

The $id should be treated as a URI #17

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 17 commits into from
Jul 25, 2023
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
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ from [kotlinx.serialization-json](https://github.com/Kotlin/kotlinx.serializatio

## Usage

### Supported targets

| Target |
|-------------------|
| jvm |
| js |
| macosX64 |
| macosArm64 |
| iosArm64 |
| iosSimulatorArm64 |
| linuxX64 |
| linuxArm64 |
| mingwX64 |

### Dependencies

#### Releases
Expand Down Expand Up @@ -112,12 +126,12 @@ val valid = schema.validate(elementToValidate, errors::add)
- [Draft 7](https://json-schema.org/specification-links.html#draft-7)
- Keywords

| Keyword | Status |
|:------------|:-----------------------------------------------------------------------------------------------------------------------|
| $id | Basic support. Only in root schema. Currently, it is interpreted as a string. Validation is in the future plans |
| $schema | There is not validation of the $schema property at the moment |
| $ref | Partially supported. Only references like _**#/path/in/schema**_ will work. The circled references validation is added |
| definitions | Supported. Definitions are loaded and can be referenced |
| Keyword | Status |
|:------------|:----------------------------------------------------------------------------------------------------|
| $id | Supported. $id in sub-schemas are collected as well and can be used in $ref |
| $schema | Supported. Validates if schema is one of the supported schemas. The last supported is used if empty |
| $ref | Supported (except references to schemas from another document) |
| definitions | Supported. Definitions are loaded and can be referenced |

- Assertions

Expand Down Expand Up @@ -156,7 +170,7 @@ val valid = schema.validate(elementToValidate, errors::add)
## Future plans

- [x] Add `$schema` property validation (if not set the latest supported will be used)
- [ ] Add proper `$id` support (for nested schemas and for referencing)
- [x] Add proper `$id` support (for nested schemas and for referencing)
- [ ] Add support for newer drafts
- [ ] [Draft 2019-09 (Draft 8)](https://json-schema.org/specification-links.html#draft-2019-09-formerly-known-as-draft-8)
- [ ] [2020-12](https://json-schema.org/specification-links.html#2020-12)
Expand Down
2 changes: 2 additions & 0 deletions api/json-schema-validator.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public final class io/github/optimumcode/json/pointer/JsonPointer$Companion {

public final class io/github/optimumcode/json/pointer/JsonPointerExtensions {
public static final fun at (Lkotlinx/serialization/json/JsonElement;Lio/github/optimumcode/json/pointer/JsonPointer;)Lkotlinx/serialization/json/JsonElement;
public static final fun contains (Lio/github/optimumcode/json/pointer/JsonPointer;Ljava/lang/String;)Z
public static final fun div (Lio/github/optimumcode/json/pointer/JsonPointer;Ljava/lang/String;)Lio/github/optimumcode/json/pointer/JsonPointer;
public static final fun get (Lio/github/optimumcode/json/pointer/JsonPointer;I)Lio/github/optimumcode/json/pointer/JsonPointer;
public static final fun plus (Lio/github/optimumcode/json/pointer/JsonPointer;Lio/github/optimumcode/json/pointer/JsonPointer;)Lio/github/optimumcode/json/pointer/JsonPointer;
public static final fun relative (Lio/github/optimumcode/json/pointer/JsonPointer;Lio/github/optimumcode/json/pointer/JsonPointer;)Lio/github/optimumcode/json/pointer/JsonPointer;
public static final fun startsWith (Lio/github/optimumcode/json/pointer/JsonPointer;Lio/github/optimumcode/json/pointer/JsonPointer;)Z
}

public final class io/github/optimumcode/json/pointer/JsonPointerKt {
Expand Down
7 changes: 1 addition & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,12 @@ kotlin {
nodejs()
}
ios()
tvos()
watchos()

val macOsTargets = listOf<KotlinTarget>(
macosX64(),
macosArm64(),
iosArm64(),
iosSimulatorArm64(),
watchosArm32(),
watchosSimulatorArm64(),
tvosArm64(),
tvosX64(),
)

val linuxTargets = listOf<KotlinTarget>(
Expand All @@ -72,6 +66,7 @@ kotlin {
val commonMain by getting {
dependencies {
api(libs.kotlin.serialization.json)
implementation(libs.uri)
}
}
val commonTest by getting {
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ nexus-publish = { id = "io.github.gradle-nexus.publish-plugin", version = "1.3.0
kotlin-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version = "1.5.1" }
kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-framework-engine = { module = "io.kotest:kotest-framework-engine", version.ref = "kotest" }
kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
uri = { group = "com.eygraber", name = "uri-kmp", version = "0.0.12" }
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,70 @@ public fun JsonPointer.relative(other: JsonPointer): JsonPointer {
}
}

/**
* Checks whether the current [JsonPointer] starts with [other].
* Returns `true` if it is so, otherwise returns `false`.
*
* **Every [JsonPointer] starts with [JsonPointer.ROOT].**
*
* **[JsonPointer.ROOT] starts only with [JsonPointer.ROOT].**
*
* Example:
* ```kotlin
* JsonPointer.ROOT.startsWith(JsonPointer("/path")) // false
* JsonPointer.ROOT.startsWith(JsonPointer.ROOT) // true
* JsonPointer("/path").startsWith(JsonPointer.ROOT) // true
* JsonPointer("/path/to/node").startsWith(JsonPointer("/path")) // true
* ```
*/
public fun JsonPointer.startsWith(other: JsonPointer): Boolean {
var primary: JsonPointer? = this
var secondary: JsonPointer? = other
while (primary != null && secondary != null) {
if (secondary is EmptyPointer) {
// secondary has finished. Means primary starts with secondary
return true
}
if (primary is EmptyPointer) {
// primary has finished but secondary is not
// means primary does not start with secondary
return false
}
primary as SegmentPointer
secondary as SegmentPointer
if (primary.propertyName != secondary.propertyName) {
return false
}
primary = primary.next
secondary = secondary.next
}
return secondary == null
}

/**
* Checks whether the [JsonPointer] contains specified [pathSegment]
*
* **[JsonPointer.ROOT] does not contain any path segments**
*
* Example:
*
* ```kotlin
* JsonPointer.ROOT.contains("path") // false
* JsonPointer("/test/path/to/node").contains("path") // true
* JsonPointer("/test/path/to/node").contains("anotherPath") // false
* ```
*/
public operator fun JsonPointer.contains(pathSegment: String): Boolean {
var segment: JsonPointer? = this
while (segment != null) {
if (segment is SegmentPointer && segment.propertyName == pathSegment) {
return true
}
segment = segment.next
}
return false
}

/**
* Extracts [JsonElement] from the current JSON element that corresponds to the specified [JsonPointer].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.optimumcode.json.schema

import io.github.optimumcode.json.pointer.JsonPointer
import io.github.optimumcode.json.schema.internal.AssertionWithPath
import io.github.optimumcode.json.schema.internal.DefaultAssertionContext
import io.github.optimumcode.json.schema.internal.JsonSchemaAssertion
import io.github.optimumcode.json.schema.internal.RefId
Expand All @@ -15,7 +16,7 @@ import kotlin.jvm.JvmStatic
*/
public class JsonSchema internal constructor(
private val assertion: JsonSchemaAssertion,
private val references: Map<RefId, JsonSchemaAssertion>,
private val references: Map<RefId, AssertionWithPath>,
) {
/**
* Validates [value] against this [JsonSchema].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ internal interface AssertionContext {

fun at(index: Int): AssertionContext
fun at(property: String): AssertionContext
fun resolveRef(refId: RefId): JsonSchemaAssertion
fun resolveRef(refId: RefId): Pair<JsonPointer, JsonSchemaAssertion>
}

internal data class DefaultAssertionContext(
override val objectPath: JsonPointer,
private val references: Map<RefId, JsonSchemaAssertion>,
private val references: Map<RefId, AssertionWithPath>,
) : AssertionContext {
override fun at(index: Int): AssertionContext = copy(objectPath = objectPath[index])

override fun at(property: String): AssertionContext {
return copy(objectPath = objectPath / property)
}

override fun resolveRef(refId: RefId): JsonSchemaAssertion {
return requireNotNull(references[refId]) { "$refId is not found" }
override fun resolveRef(refId: RefId): Pair<JsonPointer, JsonSchemaAssertion> {
val resolvedRef = requireNotNull(references[refId]) { "$refId is not found" }
return resolvedRef.schemaPath to resolvedRef.assertion
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.optimumcode.json.schema.internal

import com.eygraber.uri.Uri
import kotlin.jvm.JvmInline

@JvmInline
internal value class RefId(private val id: String) {
internal value class RefId(val uri: Uri) {
val fragment: String
get() = id.substringAfter(ROOT_REFERENCE)
get() = uri.fragment ?: ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ internal class RefSchemaAssertion(
private val basePath: JsonPointer,
private val refId: RefId,
) : JsonSchemaAssertion {
private val refIdPath: JsonPointer =
JsonPointer(refId.fragment)
private lateinit var refIdPath: JsonPointer
private lateinit var refAssertion: JsonSchemaAssertion

override fun validate(element: JsonElement, context: AssertionContext, errorCollector: ErrorCollector): Boolean {
if (!::refAssertion.isInitialized) {
refAssertion = context.resolveRef(refId)
val resolved = context.resolveRef(refId)
refIdPath = resolved.first
refAssertion = resolved.second
}
return refAssertion.validate(element, context) {
errorCollector.onError(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
package io.github.optimumcode.json.schema.internal

import io.github.optimumcode.json.pointer.JsonPointer
import io.github.optimumcode.json.pointer.contains
import io.github.optimumcode.json.pointer.startsWith

internal object ReferenceValidator {
data class ReferenceLocation(
val schemaPath: JsonPointer,
val refId: RefId,
)
fun validateReferences(references: Set<RefId>, usedRef: Set<ReferenceLocation>) {
fun validateReferences(
referencesWithPath: Map<RefId, JsonPointer>,
usedRef: Set<ReferenceLocation>,
) {
val missingRefs: Map<RefId, List<ReferenceLocation>> = usedRef.asSequence()
.filter { it.refId !in references }
.filter { it.refId !in referencesWithPath }
.groupBy { it.refId }
require(missingRefs.isEmpty()) {
"cannot resolve references: ${
missingRefs.entries.joinToString(prefix = "{", postfix = "}") { (ref, locations) ->
"\"${ref.fragment}\": ${locations.map { "\"${it.schemaPath}\"" }}"
"\"${ref.uri}\": ${locations.map { "\"${it.schemaPath}\"" }}"
}
}"
}
checkCircledReferences(usedRef)
checkCircledReferences(usedRef, referencesWithPath)
}

private val alwaysRunAssertions = hashSetOf("/allOf/", "/anyOf/", "/oneOf/")
private val alwaysRunAssertions = hashSetOf("allOf", "anyOf", "oneOf")

private fun checkCircledReferences(usedRefs: Set<ReferenceLocation>) {
val locationToRef: Map<String, String> = usedRefs.associate { (schemaPath, refId) ->
schemaPath.toString() to refId.fragment
private fun checkCircledReferences(usedRefs: Set<ReferenceLocation>, referencesWithPath: Map<RefId, JsonPointer>) {
val locationToRef: Map<JsonPointer, RefId> = usedRefs.associate { (schemaPath, refId) ->
schemaPath to refId
}

val circledReferences = hashSetOf<CircledReference>()
fun checkRunAlways(path: String): Boolean {
fun checkRunAlways(path: JsonPointer): Boolean {
return alwaysRunAssertions.any { path.contains(it) }
}
for ((location, refFragment) in locationToRef) {
val (otherLocation, otherRefFragment) = locationToRef.entries.find { (refKey) ->
val startsWith = refKey.startsWith(refFragment)
startsWith && (refKey[refFragment.length] == JsonPointer.SEPARATOR || refKey == refFragment)
for ((location, refId) in locationToRef) {
val schemaLocation: JsonPointer = referencesWithPath.getValue(refId)

val (otherLocation, otherRef) = locationToRef.entries.find { (refKey) ->
refKey.startsWith(schemaLocation)
} ?: continue
if (!location.startsWith(otherRefFragment)) {
val otherRefSchemaLocation: JsonPointer = referencesWithPath.getValue(otherRef)
if (!location.startsWith(otherRefSchemaLocation)) {
continue
}
if (checkRunAlways(location) && checkRunAlways(otherLocation)) {
circledReferences += CircledReference(
firstLocation = location,
firstRef = refFragment,
firstRef = schemaLocation,
secondLocation = otherLocation,
secondRef = otherRefFragment,
secondRef = otherRefSchemaLocation,
)
}
}
Expand All @@ -59,10 +66,10 @@ internal object ReferenceValidator {
}

private class CircledReference(
val firstLocation: String,
val firstRef: String,
val secondLocation: String,
val secondRef: String,
val firstLocation: JsonPointer,
val firstRef: JsonPointer,
val secondLocation: JsonPointer,
val secondRef: JsonPointer,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Loading