Code Monkey home page Code Monkey logo

json-schema-scala's Introduction

JSON Schema for Scala

This library describes JSON Schema documents in Scala and provides support for turning Scala data-types into JSON Schema documents automatically using Magnolia. This library does not attempt to implement all the features of JSON Schema (and currently only targets draft 7).

Usage

You can build JSON Schema documents yourself from scratch, or you can use the JSON Schema Encoder typeclass which will automatically build a JSON Schema document from most Scala data-types. For example:

import io.kaizensolutions.jsonschema._

@title("A representation of a Person")
final case class Person(
  @description("age of the person in years") age: Option[Int],
  @description("full name of the person") name: String,
  device: Device
)

sealed trait Device
object Device {
  private final case class Simple(id: Int, reading: Reading)       extends Device
  private final case class Cluster(groupId: Int, reading: Reading) extends Device

  def cluster(groupId: Int, reading: Reading): Device = Cluster(groupId, reading)
  def simple(id: Int, reading: Reading): Device = Simple(id, reading)
}

final case class Reading(
  @description("poll frequency") pollFreq: Long,
  last: Option[Int]
)

You can automatically derive a JSON Schema document for a Person like so:

val jssDoc: JsonSchemaDocument = 
  JsonSchemaEncoder[Person].encode

JsonSchemaDocument is a Scala data-type that you can further manipulate if you like. You can also turn it into JSON using one of the renderers (like circe):

import io.kaizensolutions.jsonschema.renderers.circe._

jssDoc.toJson.spaces2

This will generate the following JSON Schema document:

Click here to see the document rendered in JSON

{
  "$schema" : "http://json-schema.org/draft-07/schema#",
  "$id" : "io.kaizensolutions.jsonschema.Person",
  "type" : "object",
  "properties" : {
    "age" : {
      "type" : "integer",
      "description" : "age of the person in years"
    },
    "name" : {
      "type" : "string",
      "description" : "full name of the person"
    },
    "device" : {
      "$ref" : "io.kaizensolutions.jsonschema.Device"
    }
  },
  "required" : [
    "name",
    "device"
  ],
  "additionalProperties" : true,
  "definitions" : {
    "io.kaizensolutions.jsonschema.Reading" : {
      "$id" : "io.kaizensolutions.jsonschema.Reading",
      "type" : "object",
      "properties" : {
        "pollFreq" : {
          "type" : "number",
          "description" : "poll frequency"
        },
        "last" : {
          "type" : "integer"
        }
      },
      "required" : [
        "pollFreq"
      ],
      "additionalProperties" : true
    },
    "io.kaizensolutions.jsonschema.Device" : {
      "$id" : "io.kaizensolutions.jsonschema.Device",
      "oneOf" : [
        {
          "type" : "object",
          "properties" : {
            "groupId" : {
              "type" : "integer"
            },
            "reading" : {
              "$ref" : "io.kaizensolutions.jsonschema.Reading"
            }
          },
          "required" : [
            "groupId",
            "reading"
          ],
          "additionalProperties" : true
        },
        {
          "type" : "object",
          "properties" : {
            "id" : {
              "type" : "integer"
            },
            "reading" : {
              "$ref" : "io.kaizensolutions.jsonschema.Reading"
            }
          },
          "required" : [
            "id",
            "reading"
          ],
          "additionalProperties" : true
        }
      ]
    }
  },
  "title" : "A representation of a Person"
}

Inspiration

This library is heavily inspired by Andriy Onyshchuk's amazing JSON Schema library which is based on macros. This library takes a slightly different approach and uses Magnolia instead to do typeclass derivation in the hopes of making the Scala 3 migration process a lot easier.

json-schema-scala's People

Contributors

calvinlfer avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.