scala-jsonschema
scala-jsonschema copied to clipboard
Question: How to create json schema from a case class, and create custom schemas for specific types
Hi, First of all, thank you for the great library !!
-
I have a specific use case in which I want to create a schema for a case class in which most of the types are known and can be created automatically, for example: String, Int , etc. But for some other types I want to create a custom schema manually, how can I achieve that?
-
In addition I want to create a custom schema and change the field name, for example: case class CusotmClass(unsafeMap: Map[String, Int]) in the json the case class will actually look like the following, instead of "unsafeMap" it will be written "flags".
"flags" : { "a1" : 1, "a2" : 2 }
- How can I create custom scehma for FiniteDuration ?
Thanks :)
hello @hagay3 would be much easier if you'd send me a link to your https://scastie.scala-lang.org/ playground with classes/types you want to cover
but in general for case 1 what you need to do is make sure that manually created schemas for your types have been put into an implicit scope when the json schema for the main case class is getting generated
for the 2: there is no way to make code generator generate a json schema model containing renamed field, but you can update the model on your own
say
case class Foo(foo: String)
val fooSchema = Json.objectSchema[Foo].withFieldsUpdated {
case f if f.name == "foo" => f.copy(name = "bar")
}
for the 3 i'd recommend to take a quick look at https://github.com/andyglow/scala-jsonschema/blob/master/modules/joda-time/src/main/scala/com/github/andyglow/jsonschema/JodaTimeSupport.scala
cheers
@andyglow Thank you for your kind help, the details were pretty helpful!