Skip to content

Instantly share code, notes, and snippets.

@ryanpbrewster
Created May 12, 2016 22:34
Show Gist options
  • Select an option

  • Save ryanpbrewster/d0dce0e1efc2f40fd9fb41ae783750f0 to your computer and use it in GitHub Desktop.

Select an option

Save ryanpbrewster/d0dce0e1efc2f40fd9fb41ae783750f0 to your computer and use it in GitHub Desktop.
sealed abstract class JsonSchema {
def asJson: JsValue
}
object JsonSchema {
final case class Single(description: String) extends JsonSchema { def asJson = JsString(description) }
final case class Optional(field: JsonSchema) extends JsonSchema { def asJson = field.asJson }
final case class Array(kind: JsonSchema) extends JsonSchema { def asJson = Json.arr(kind.asJson) }
final case class Object(fields: Seq[(String, JsonSchema)]) extends JsonSchema {
def asJson = JsObject(fields.map {
case (name, JsonSchema.Optional(schema)) => (name + "?") -> schema.asJson
case (name, schema) => name -> schema.asJson
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment