Skip to content

OptimumCode/json-schema-validator

Repository files navigation

json-schema-validator

Licence

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

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.1")
Groovy
repositories { mavenCentral() } implementation 'io.github.optimumcode:json-schema-validator:0.0.1'

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.1-SNAPSHOT")
Groovy
repositories { maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' } } implementation 'io.github.optimumcode:json-schema-validator:0.0.1-SNAPSHOT'

Example

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)

Supported JSON schema drafts:

  • Draft 7

    • Keywords
    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
    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

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
  • Formalize error output as it is defined in the latest drafts (have not fully decided if it should be done)

About

The JSON schema validation library that works with https://github.com/Kotlin/kotlinx.serialization

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 5