Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transform of multi-spec to JSON Schema #281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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