Skip to content

Commit

Permalink
Support transform of multi-spec to JSON Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
werenall committed Jan 9, 2024
1 parent 7a3b59e commit d4d97ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/spec_tools/json_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
(defmethod accept-spec 'spec-tools.core/merge [_ spec children options]
(accept-merge children spec options))

(defmethod accept-spec 'clojure.spec.alpha/multi-spec [_ _ children _]
{:anyOf children})

(defmethod accept-spec 'clojure.spec.alpha/every [_ spec children options]
(let [form (impl/extract-form spec)
{:keys [type]} (parse/parse-spec form)]
Expand Down
6 changes: 6 additions & 0 deletions src/spec_tools/visitor.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
(defmethod visit-spec 'spec-tools.core/merge [spec accept options]
(visit-merge spec accept options))

(defmethod visit-spec 'clojure.spec.alpha/multi-spec [spec accept options]
(let [methods-specs (->> (impl/extract-form spec)
(parse/get-multi-spec-sub-specs)
(into {}))]
(accept 'clojure.spec.alpha/multi-spec spec (mapv #(visit (val %) accept options) methods-specs) options)))

(defmethod visit-spec 'clojure.spec.alpha/every [spec accept options]
(let [[_ inner-spec] (impl/extract-form spec)]
(accept 'clojure.spec.alpha/every spec [(visit inner-spec accept options)] options)))
Expand Down
22 changes: 22 additions & 0 deletions test/cljc/spec_tools/json_schema_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
(s/def ::keys-no-req (s/keys :opt [::e]
:opt-un [::e]))

(defmulti event-payload :action)

(s/def :event.payload.add/action #{:add})
(s/def :event.payload.add/payload int?)

(defmethod event-payload :add
[_]
(s/keys :req-un [:event.payload.add/action :event.payload.add/payload]))

(s/def :event.payload.result/action #{:result})
(s/def :event.payload.result/payload nil?)

(defmethod event-payload :result
[_]
(s/keys :req-un [:event.payload.result/action]
:opt-un [:event.payload.result/payload]))

(deftest simple-spec-test
(testing "primitive predicates"
;; You're intented to call jsc/to-json with a registered spec, but to avoid
Expand Down Expand Up @@ -99,6 +116,11 @@
:properties {"spec-tools.json-schema-test/integer" {:type "integer"}
"spec-tools.json-schema-test/string" {:type "string"}}
:required ["spec-tools.json-schema-test/integer" "spec-tools.json-schema-test/string"]}))
(is (= (jsc/transform (s/multi-spec event-payload :action))
{:anyOf [{:type "object" :properties {"action" {:enum [:result]} "payload" {:type "null"}} :required ["action"]}
{:type "object"
:properties {"action" {:enum [:add]} "payload" {:type "integer" :format "int64"}}
:required ["action" "payload"]}]}))
(is (= (jsc/transform (s/every integer?)) {:type "array" :items {:type "integer"}}))
(is (= (jsc/transform (s/every-kv string? integer?))
{:type "object" :additionalProperties {:type "integer"}}))
Expand Down

0 comments on commit d4d97ae

Please sign in to comment.