JSON schema (2020-12) to scala case classes

Viewed 50

I am unable to find any library to convert nested JSON schema(2020-12) to scala case classes for scala project. I am using api first approach, so its necessary. Found few projects supporting scala case class to JSON Schema but none for JSON schema to scala case class.

Looking for something like this

Example JSON:

  {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/product.schema.json",
  "title": "Product",
  "type": "object",
  "properties": {
    "productId": {
      "type": "integer"
    },
    "tags": {
      "description": "Tags for the product",
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 1,
      "uniqueItems": true
    },
    "dimensions": {
      "type": "object",
      "properties": {
        "length": {
          "type": "number"
        },
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      },
      "required": [
        "length"
      ]
    }
  },
  "required": [
    "productId",
    "price"
  ]
}

Converted to:

    case class Product (
     productId: Int,
     tags: Option[List[String]],
     dimensions: Option[Dimensions]
){
    assert( tags.get.length >= 1,  "`tags.get` violates 'minItems' constraint" )
    assert( tags.get.length == tags.get.distinct.length,  "`tags.get` violates 'uniqueItems' constraint" )
}


/**
 * @param length
 * @param width
 * @param height
 */
case class Dimensions (
     length: Double,
     width: Option[Double],
     height: Option[Double]
)
0 Answers
Related