Overriding Keys' Values in Map[ADT, ...]? #245
-
The following code: package net
import com.github.andyglow.json.ToValue
import json.{Schema, Json => AndyGlowJson}
import com.github.andyglow.jsonschema.AsCirce
import com.github.andyglow.jsonschema.AsCirce._
import io.circe.{Codec, Decoder, DecodingFailure, Encoder, Json, KeyDecoder, KeyEncoder}
import json.schema.Version._
import io.circe.generic.semiauto
import json.schema.{title, typeHint}
import io.circe.generic.extras.semiauto._
import io.circe.generic.extras.Configuration
@typeHint[String]
sealed trait Discriminator
object Discriminator {
@typeHint[String]
case object X extends Discriminator {
override def toString: String = "B"
}
@typeHint[String]
case object Y extends Discriminator {
override def toString: String = "C"
}
}
case class Something(a: String)
case class Foo(m: Map[Discriminator, List[Something]])
object Foo {
val schema = AndyGlowJson.schema[Foo].asCirce(Draft04())
} outputs the following JSON Schema: scala> net.Foo.schema.spaces2
res0: String =
{
"properties" : {
"m" : {
"type" : "object",
"patternProperties" : {
"^(?:X|Y)$" : {
"type" : "array",
"items" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"a" : {
"type" : "string"
}
},
"required" : [
"a"
]
}
}
}
}
},
"additionalProperties" : false,
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"required" : [
"m"
]
} I'd like the JSON Schema to have Thanks for your consideration and this library! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Well.. in your case you can use Try
|
Beta Was this translation helpful? Give feedback.
Well..
override def toString: String = "B"
is the run-time constructionwhereas
scala-jsonschema
was designed as compile-time code generator..which means all the info about the schema is taken from types
in your case you can use
KeyPattern[T]
typeclass which would override the string used for patternProperties key definition.Try