Skip to content

Commit

Permalink
Merge pull request #281 from RucaHQ/support-multispec-json-schema-tra…
Browse files Browse the repository at this point in the history
…nsforms

Support transform of multi-spec to JSON Schema
  • Loading branch information
ikitommi authored Jun 29, 2024
2 parents ec02b0a + ba12bd5 commit ea023e5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Support transforming schema to JSON Schema. PR [#281](https://github.com/metosin/spec-tools/pull/281)

# 0.10.6 (2023-08-28)

* Deprecate `spec-tools.openapi/openapi-spec`
Expand Down
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 ea023e5

Please sign in to comment.