Skip to content

Instantly share code, notes, and snippets.

@rymndhng
Last active August 8, 2023 16:25
Show Gist options
  • Save rymndhng/4a780218cb2852de2922 to your computer and use it in GitHub Desktop.
Save rymndhng/4a780218cb2852de2922 to your computer and use it in GitHub Desktop.
Prismatic Schema to Json Schema

Prismatic Schema to Json Schema

Simple parser that takes Prismatic/Schema in and dumps JsonSchema out. Waow!

#!/usr/bin/env boot
(set-env!
:source-paths #{"src"}
:resource-paths #{"resources"}
:dependencies '[[org.clojure/clojure "1.8.0"]
[prismatic/schema "1.0.5"]
[org.clojure/data.json "0.2.6"]])
(ns schema
(:require [schema.core :as s]
[clojure.data.json :as json]))
(s/defrecord SomethingElse
[id :- s/Uuid
config :- (s/maybe s/Str)
page_id :- [s/Uuid]
type :- (s/enum "webhook" "something_else")])
(s/defrecord
Integration
[id :- s/Uuid
config :- (s/maybe s/Str)
page_id :- [s/Uuid]
metadata :- SomethingElse
type :- (s/enum "webhook" "something_else")])
(s/explain Integration)
;; -- Output -------------------------------------------------------------------
(record schema.Integration {:id Uuid,
:config (maybe Str),
:page_id [Uuid],
:metadata (record schema.SomethingElse {:id Uuid,
:config (maybe Str),
:page_id [Uuid],
:type (enum "something_else" "webhook")}),
:type (enum "something_else" "webhook")})
;; -- Plumbing Code ------------------------------------------------------------
(defn plumatic-schema->json-schema [schema]
"One way conversion from rich data into json schema."
(cond (vector? schema)
{:type "array"
:items (plumatic-schema->json-schema (first schema))}
(seq? schema)
(condp = (first schema)
'record {:$ref (str (clojure.string/replace (second schema) "." "/") ".json")}
'maybe (plumatic-schema->json-schema (second schema))
'enum {:type "string"
:enum (rest schema)})
:default
{:type (condp = schema
'Str "string"
'Uuid "string")}))
(defn write-json-schema [schema]
(let [[type symbol spec] (s/explain schema)
required (filter #(not (is-optional (second %))) spec)]
{:$schema "http://json-schema.org/draft-04/schema#"
:description "Figure out how to assign this meta from defschema"
:type "object" ,
:required (map first required) ;only use the keys
:properties (into {} (for [[k v] spec]
[k (plumatic-schema->json-schema v)]))}))
;; -- DEMO ---------------------------------------------------------------------
(println (-> Integration
write-json-schema
json/write-str))
;; -- OUTPUT --------------------------------------------------------------
(comment
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Figure out how to assign this meta from defschema",
"type": "object",
"required": [
"id",
"page_id",
"metadata",
"type"
],
"properties": {
"id": {
"type": "string"
},
"config": {
"type": "string"
},
"page_id": {
"type": "array",
"items": {
"type": "string"
}
},
"metadata": {
"$ref": "schema/SomethingElse.json"
},
"type": {
"type": "string",
"enum": [
"something_else",
"webhook"
]
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment