Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created May 3, 2019 09:09
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 jmglov/315fbc8eb039ca90e5d9f865197484c5 to your computer and use it in GitHub Desktop.
Save jmglov/315fbc8eb039ca90e5d9f865197484c5 to your computer and use it in GitHub Desktop.
(defmethod json-schema/accept-spec 'clojure.spec.alpha/keys [_ spec children _]
(let [name (comp #(clojure.string/replace % "-" "_") name)
{:keys [req req-un opt opt-un]} (impl/parse-keys (impl/extract-form spec))
names-un (map name (concat req-un opt-un))
names (map impl/qualified-name (concat req opt))
required (map impl/qualified-name req)
required-un (map name req-un)
all-required (not-empty (concat required required-un))]
(#'json-schema/maybe-with-title
(merge
{:type "object"
:properties (zipmap (concat names names-un) children)}
(when all-required
{:required (vec all-required)}))
spec)))
;; https://github.com/metosin/spec-tools/pull/110
(defmethod json-schema/accept-spec :spec-tools.visitor/spec [_ spec children _]
(let [[_ data] (spec-tools.impl/extract-form spec)
json-schema-meta (reduce-kv
(fn [acc k v]
(if (= "json-schema" (namespace k))
(assoc acc (keyword (name k)) v)
acc))
{}
(into {} data))
extra-info (-> data
(select-keys [:name :description])
(set/rename-keys {:name :title}))
m (merge (spec-tools.impl/unwrap children)
extra-info
json-schema-meta)
is-ref-arr (and (:items-ref json-schema-meta) (= "array" (:type m)))]
(cond
(:ref json-schema-meta) {"$ref" (:ref json-schema-meta)}
is-ref-arr (-> m
(assoc :items {"$ref" (:items-ref json-schema-meta)})
(dissoc :items-ref))
:else m)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment