Skip to content

Instantly share code, notes, and snippets.

@mavnn
Created December 17, 2015 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavnn/b8c48070e249818b21a4 to your computer and use it in GitHub Desktop.
Save mavnn/b8c48070e249818b21a4 to your computer and use it in GitHub Desktop.
Extendable Json schema
type PropertyName = string
type Schema =
{ title : string option
description : string option
``type`` : JsonType option
items : Schema option
properties : Map<PropertyName, Schema> option
extensions : Map<string, Json> }
static member ToJson (schema : Schema) =
let jsonId = fun json -> Value (), json
let writeKnown =
maybeWrite "title" schema.title
*> maybeWrite "description" schema.description
*> maybeWrite "type" schema.``type``
*> maybeWrite "items" schema.items
*> maybeWrite "properties" schema.properties
schema.extensions
|> Map.toList
|> List.map (fun (k, j) -> Json.write k j)
|> List.fold ( *>) writeKnown
static member FromJson (_ : Schema) : Json<Schema> =
fun json ->
let extensions =
match json with
| Json.Object o ->
["title";"description";"type";"items";"properties"]
|> List.fold (fun m k -> Map.remove k m) o
| _ -> failwithf "Expected json schema, recieved %A" json
((fun tit d tp is ps ->
{ title = tit
description = d
``type`` = tp
items = is
properties = ps
extensions = extensions
})
<!> Json.tryRead "title"
<*> Json.tryRead "description"
<*> Json.tryRead "type"
<*> Json.tryRead "items"
<*> Json.tryRead "properties") json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment