Skip to content

hamnis/jsonschema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

JSON Schema

Maven Central

Implements a Json Schema[A] using sttp-apispec and Circe.

Circe Decoders and Encoders are derived from the schema definition.

Background

The idea for this has been in part been due to Fabio Labella's Dynosaur and I have stolen the Prism class from that repo to avoid adding a dependency to monocle.

Usage

To use this include a dependency to:

libraryDependencies += "net.hamnaberg" %% "jsonschema-core" % "version"  

Example

import net.hamnaberg.schema._

case class Person(name: String, age: Int)

object Person {
  implicit val schema: Schema[Person] = Schema.record[Person] { field =>
    (
      field[String]("name", _.name),
      field[Int]("age", _.age)(Schema.boundedInt(Bounds.both(Bound.Inclusive(0), Bound.Exclusive(150))))
      ).mapN(Person.apply)
  }
}

import io.circe._
import io.circe.syntax._

object Main {
  def main(args: Array[String]) = {
    val json = Json.obj("name" := "Erlend", "age" := 40)
    val Right(result) = schema.decode(json)
    println(result)
  }
}

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages