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

Conversation

werenall
Copy link
Contributor

@werenall werenall commented Jan 9, 2024

(defmulti event-type :action)

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

(defmethod event-type :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-type :result
  [_]
  (s/keys :req-un [:event.payload.result/action]
          :opt-un [:event.payload.result/payload]))

(s/def ::event.payload
  (s/multi-spec event-type :action))
=> nil
=> :event.payload.add/action
=> :event.payload.add/payload
=> #object[clojure.lang.MultiFn 0x2a66cd26 "clojure.lang.MultiFn@2a66cd26"]
=> :event.payload.result/payload
=> :event.payload.result/action
=> #object[clojure.lang.MultiFn 0x2a66cd26 "clojure.lang.MultiFn@2a66cd26"]
=> :damian-test/event.payload
(json-schema/transform ::event.payload)
=>
{: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"]}]}

IMO it's best as a convention to add specs like (s/def :event.payload.add/action #{:add}). It might look surprising in the beginning. After all, it's already enforced by the multimethod mechanisms. But making that extra step prevents two issues:

  1. the json schema is incapable of picking up the quirks of complex dispatch-fns of multimethods. So without it the resulting json schema would be incomplete.
  2. whenever you need to use spec generators to create samples, the methods again won't pickup the logic without that extra step.
(gen/sample (s/gen ::event.payload))
=>
({:payload nil, :action :result}
 {:action :result}
 {:action :add, :payload 0}
 {:payload nil, :action :result}
 {:action :add, :payload -1}
 {:payload nil, :action :result}
 {:action :add, :payload -9}
 {:action :add, :payload -1}
 {:action :result}
 {:action :add, :payload -104})

@werenall werenall force-pushed the support-multispec-json-schema-transforms branch from d4d97ae to 2bcf5a6 Compare January 10, 2024 08:37
@werenall werenall force-pushed the support-multispec-json-schema-transforms branch from 2bcf5a6 to ba12bd5 Compare January 10, 2024 08:38
@ikitommi
Copy link
Member

LGTM, thanks!

@ikitommi ikitommi merged commit ea023e5 into metosin:master Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants