Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lowmelvin/scala-json-schema
Scala to Json Schema. Lite.
https://github.com/lowmelvin/scala-json-schema
circe json-schema scala scala3
Last synced: 28 days ago
JSON representation
Scala to Json Schema. Lite.
- Host: GitHub
- URL: https://github.com/lowmelvin/scala-json-schema
- Owner: lowmelvin
- Created: 2024-09-08T19:37:26.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2024-09-18T18:08:45.000Z (about 2 months ago)
- Last Synced: 2024-09-26T19:05:04.600Z (about 1 month ago)
- Topics: circe, json-schema, scala, scala3
- Language: Scala
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Scala Json Schema (Lite)
This is a minimal library to generate JSON schema from Scala ADTs.
It is built on top of [circe](https://circe.github.io/circe/).To reduce complexity and maintenance, this library
does the absolute minimum: It will traverse your ADT and generate
a schema with nested types and properties. No other fields are included.Product types get the type `object` and sum types are converted into `anyOf`.
You can inject all other fields via annotation.
As intended, there is zero validation--you can do whatever you want.## Installation
This library is built for Scala 3.
```scala
libraryDependencies += "com.melvinlow" %% "scala-json-schema" % ""
```## Examples:
Encode a product:
```scala
import com.melvinlow.json.schema.*
import com.melvinlow.json.schema.generic.auto.givencase class Foo(x: Int, y: String)
JsonSchema[Foo].spaces2
// res0: String = """{
// "type" : "object",
// "properties" : {
// "x" : {
// "type" : "integer"
// },
// "y" : {
// "type" : "string"
// }
// }
// }"""
```Encode a coproduct:
```scala
enum Bar:
case A, BJsonSchema[Bar].spaces2
// res1: String = """{
// "anyOf" : [
// {
// "type" : "object",
// "properties" : {
//
// }
// },
// {
// "type" : "object",
// "properties" : {
//
// }
// }
// ]
// }"""
```Add fields via annotation (it takes as input any `String` and `Json` key-value pair):
```scala
import com.melvinlow.json.schema.annotation.JsonSchemaField
import io.circe.Json
import io.circe.syntax.*@JsonSchemaField("title", "dog".asJson)
@JsonSchemaField("required", Array("name").asJson)
case class Dog(
@JsonSchemaField("minLength", 1.asJson)
name: String
)JsonSchema[Dog].spaces2
// res2: String = """{
// "required" : [
// "name"
// ],
// "title" : "dog",
// "type" : "object",
// "properties" : {
// "name" : {
// "minLength" : 1,
// "type" : "string"
// }
// }
// }"""
```Create an encoder for a new type:
```scala
object H:
@JsonSchemaField("description", "my custom int".asJson)
opaque type MyInt = Intgiven JsonSchemaEncoder[H.MyInt] with
def schema: Json =
val base = Json.obj("type" -> "integer".asJson)
val annotations = JsonSchemaField.onType[H.MyInt]
base.deepMerge(Json.obj(annotations*))JsonSchema[H.MyInt].spaces2
// res3: String = """{
// "description" : "my custom int",
// "type" : "integer"
// }"""
```