Skip to content

Release 0.0.1

Compare
Choose a tag to compare
@OptimumCode OptimumCode released this 23 Jul 09:52
· 345 commits to main since this release
73fa548

This is the first release of the multiplatform JSON-schema-validator library.

🚀 Features

  • Support for Draft 7
    • All assertion keywords are supported
    • Most of the schema keywords are supported
      • $id works only for the root schema
      • $schema value is not used
      • $ref works only for relative references (like #/definitions/negativeInteger)

Usage

Add library to your dependencies

implementation("io.github.optimumcode:json-schema-validator:0.0.1")

Create a schema and validate the JSON element you need

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)

Future

This library is in the early stage of development so more changes and features are on the way.
Some of them you can find in the README and opened issues.

If you have any questions/suggestions/problems, please, create an issue on GitHub and I will try to help you