Skip to content

OptimumCode/json-schema-validator

Repository files navigation

json-schema-validator

Licence Supported-drafts Codecov

Maven Central Version Sonatype Nexus (Releases) Sonatype Nexus (Snapshots)

This multiplatform library is an implementation of JSON schema that can validate JsonElement from kotlinx.serialization-json library.

Open in GitHub Codespaces Open in DevPod!

Usage

Supported targets

Target
jvm
js
macosX64
macosArm64
iosArm64
iosSimulatorArm64
linuxX64
linuxArm64
mingwX64

Dependencies

Releases

In order to use releases add Maven Central repository to the list of repositories.

Kotlin
repositories {
  mavenCentral()
}

implementation("io.github.optimumcode:json-schema-validator:0.0.13")
Groovy
repositories {
  mavenCentral()
}

implementation 'io.github.optimumcode:json-schema-validator:0.0.13'

Release are published to Sonatype repository. The synchronization with Maven Central takes time. If you want to use the release right after the publication you should add Sonatype Release repository to your build script.

Kotlin
repositories {
  maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
}
Groovy
repositories {
  maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' }
}

Snapshots

If you want to use SNAPSHOT version you should add Sonatype Snapshot repository to your build script.

Kotlin
repositories {
  maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots")
}

implementation("io.github.optimumcode:json-schema-validator:0.0.14-SNAPSHOT")
Groovy
repositories {
  maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
}

implementation 'io.github.optimumcode:json-schema-validator:0.0.14-SNAPSHOT'

Example

If you have just one JSON schema or many independent schemes you can create it using factory methods defined on JsonSchema class.

import io.github.optimumcode.json.schema.JsonSchema
import io.github.optimumcode.json.schema.ValidationError
import kotlinx.serialization.json.JsonElement

val key = "\$" // to use $ in multiline string
val schema = JsonSchema.fromDefinition(
  """
  {
    "${key}schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
      "positiveInteger": {
        "type": "integer",
        "minimum": 0
      }
    },
    "properties": {
      "size": { "${key}ref": "#/definitions/positiveInteger" }
    }
  }
  """.trimIndent(),
)
val errors = mutableListOf<ValidationError>()
val elementToValidate: JsonElement = loadJsonToValidate()

val valid = schema.validate(elementToValidate, errors::add)

If you need to use more than one schema, and they have references to other schemas you should use JsonSchemaLoader class.

import io.github.optimumcode.json.schema.JsonSchemaLoader
import io.github.optimumcode.json.schema.JsonSchema
import io.github.optimumcode.json.schema.ValidationError
import kotlinx.serialization.json.JsonElement

val schema: JsonSchema = JsonSchemaLoader.create()
  .register(
    """
    {
      "${KEY}id": "https://test.com",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
    """.trimIndent(),
  ).fromDefinition(
    """
    {
      "properties": {
        "anotherName": {
          "${KEY}ref": "https://test.com#/properties/name"
        }
      }
    }
    """.trimIndent(),
  )

val errors = mutableListOf<ValidationError>()
val elementToValidate: JsonElement = loadJsonToValidate()

val valid = schema.validate(elementToValidate, errors::add)

Supported JSON schema drafts:

  • Draft 7

    Supported keywords
    • Core
    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
    definitions Supported. Definitions are loaded and can be referenced
    • Assertions
    Category Assertion Status
    General type Supported all type defined in the specification
    enum Supported
    const Supported
    Numbers multipleOf Supported
    maximum Supported
    exclusiveMaximum Supported
    minimum Supported
    exclusiveMinimum Supported
    Strings maxLength Supported
    minLength Supported
    pattern Supported (kotlin.text.Regex is used)
    Arrays items Supported
    additionalItems Supported
    maxItems Supported
    uniqueItems Supported
    contains Supported
    Objects maxProperties Supported
    minProperties Supported
    required Supported
    properties Supported
    patternProperties Supported (kotlin.text.Regex is used)
    additionalProperties Supported
    dependencies Supported
    propertyNames Supported
    Conditions if/then/else Supported
    Boolean logic allOf Supported
    anyOf Supported (all validation will be executed even if the element matches the first one)
    oneOf Supported
    not Supported
  • Draft 2019-09

    Supported keywords
    • Core
    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
    $recursiveRef Supported
    $defs/definitions Supported. Definitions are loaded and can be referenced
    $vocabulary Supported. You can disable and enable vocabularies through custom meta-schemes
    • Assertions
    Category Assertion Status
    General type Supported all type defined in the specification
    enum Supported
    const Supported
    Numbers multipleOf Supported
    maximum Supported
    exclusiveMaximum Supported
    minimum Supported
    exclusiveMinimum Supported
    Strings maxLength Supported
    minLength Supported
    pattern Supported (kotlin.text.Regex is used)
    Arrays items Supported
    additionalItems Supported
    unevaluatedItems Supported
    maxItems Supported
    uniqueItems Supported
    contains Supported
    minContains Supported (does not affect the work of contains assertion anyhow even if minContains=0)
    maxContains Supported
    Objects maxProperties Supported
    minProperties Supported
    required Supported
    properties Supported
    patternProperties Supported (kotlin.text.Regex is used)
    additionalProperties Supported
    unevaluatedProperties Supported
    dependentRequired Supported
    dependentSchemas Supported
    propertyNames Supported
    Conditions if/then/else Supported
    Boolean logic allOf Supported
    anyOf Supported (all validation will be executed even if the element matches the first one)
    oneOf Supported
    not Supported
  • Draft 2020-12

    Supported keywords
    • Core
    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
    $dynamicRef/$dynamicAnchor Supported
    $defs/definitions Supported. Definitions are loaded and can be referenced
    $vocabulary Supported. You can disable and enable vocabularies through custom meta-schemes
    • Assertions
    Category Assertion Status
    General type Supported all type defined in the specification
    enum Supported
    const Supported
    Numbers multipleOf Supported
    maximum Supported
    exclusiveMaximum Supported
    minimum Supported
    exclusiveMinimum Supported
    Strings maxLength Supported
    minLength Supported
    pattern Supported (kotlin.text.Regex is used)
    Arrays prefixItems Supported
    items Supported
    unevaluatedItems Supported
    maxItems Supported
    uniqueItems Supported
    contains Supported
    minContains Supported
    maxContains Supported
    Objects maxProperties Supported
    minProperties Supported
    required Supported
    properties Supported
    patternProperties Supported (kotlin.text.Regex is used)
    additionalProperties Supported
    unevaluatedProperties Supported
    dependentRequired Supported
    dependentSchemas Supported
    propertyNames Supported
    Conditions if/then/else Supported
    Boolean logic allOf Supported
    anyOf Supported (all validation will be executed even if the element matches the first one)
    oneOf Supported
    not Supported

Format assertion

The library supports format assertion. Not all formats are supported yet. The supported formats are:

  • date
  • time
  • date-time
  • duration
  • json-pointer
  • relative-json-pointer
  • ipv4
  • ipv6
  • uuid
  • hostname
  • idn-hostname
  • uri
  • uri-reference
  • uri-template
  • iri
  • iri-reference

But there is an API to implement the user's defined format validation. The FormatValidator interface can be user for that. The custom format validators can be register in JsonSchemaLoader.

Please note, that the format validation API is marked as experimental and will require OptIn declaration in your code.

Custom assertions

You can implement custom assertions and use them. Read more here.

Compliance to JSON schema test suites

This library uses official JSON schema test suites as a part of the CI to make sure the validation meet the expected behavior. Not everything is supported right now but the missing functionality might be added in the future. The tests are located here.

NOTE: Python 3.* is required to run test-suites. It is used to generate list of remote schemas using this script

This library is also integrated into bowtie and runs against the official test suite along with other libraries. You can find the report here.

draft-07 draft/2019-09 draft/2020-12

Benchmarking

There is a benchmark project that compares this library with some other ones:

The benchmark is scheduled to run every night on Monday. You can see the results in the latest workflow execution.

Developer notes

Build process

The update to Kotlin 1.9.22 came with an issue for JS incremental compilation. In case you see an error about main function that already bind please execute clean task.

When you build project for linux target you might get an error about missing native library. This is because de.cketti.unicode:kotlin-codepoints requires this library to perform string normalization. This is needed to support idn-hostname format. Install this library with the following command:

sudo apt-get install -y libunistring-dev

Devcontainer

Devcontainers is a cool feature. However, by default in Codespaces and DevPod you will use VS Code. This is a good IDE but not for Kotlin, unfortunately. The extension that is available for VS Code to support Kotlin works quite slow (when workspace is just started) and sometimes does not work correctly with multiplatform definitions. Because of that I higly recoment using JetBrains Gateway (but it looks like the GitHub Codespace provider is not currently compatible with latest Gateway version). However, there is a way to connect to Codespace and work with project using JetBrains IDE. Please, read details here.

Future plans

  • Add $schema property validation (if not set the latest supported will be used)
  • Add proper $id support (for nested schemas and for referencing)
  • Add support for newer drafts
  • Add support for schemas from external documents
    • Load schemas from local sources
    • Load schemas from remote sources
  • Formalize error output as it is defined in the latest drafts (have not fully decided if it should be done)