metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 204 forks source link

function schemas that can use input values in output schemas #936

Closed stevebuik closed 5 months ago

stevebuik commented 10 months ago
(mx/defn tall-people :- [:vector [:map [:height :int]]]
  [{:keys [min-height]} :- [:map [:min-height :int]]]
  [{:height 200}
   {:height 150}])

(tall-people {:min-height 180})

=> should throw exception when instrumented

spec1 s/fdef can do this so parity or better would be ideal. what are strengths/weaknesses of s/fdef ?

stevebuik commented 10 months ago

thanks to chatgpt, here's the fdef equivalent for comparison

(s/def ::height int?)
(s/def ::min-height int?)
(s/def ::person (s/keys :req-un [::height]))
(s/def ::tall-people-args (s/keys :req-un [::min-height]))
(s/def ::tall-people-result (s/coll-of ::person :kind vector?))

(defn tall-people [args]
  (let [{:keys [min-height]} args]
    [{:height 200}
     {:height 150}]))

(s/fdef tall-people
        :args (s/cat :args ::tall-people-args)
        :ret (s/coll-of ::person :kind vector?)
        :fn (fn [{:keys [args ret]}]
              (let [{:keys [min-height]} (:args args)]
                (every? #(>= (:height %) min-height) ret))))

(stest/instrument)

(tall-people {:min-height 180})

although return values are not checked even when instrumented I believe

I hope Malli can be more terse. what other improvements can we make on this?

ikitommi commented 10 months ago

this can be made to work:

(def Input [:map [:min-height :int]])

(defn guard [{:keys [input output]}]
  (every? #(>= (:height %) (:min-height input)) output))

(mx/defn kikka :- [:vector Input]
  {:guard guard}
  [_ :- Input]
  ;; body
  [{:height 200}
   {:height 150}])
stevebuik commented 10 months ago

Looks good. Can the Input and other schemas be provided by the usual options with a registry with this design?

We have moved to using registries everywhere

On Thu, 24 Aug 2023, 8:50 pm Tommi Reiman, @.***> wrote:

this can be made to work:

(def Input [:map [:min-height :int]])

(defn guard [{:keys [input output]}] (every? #(>= (:height %) (:min-height input)) output))

(mx/defn kikka :- [:vector Input] {:fn guard} [_ :- Input] ;; body [{:height 200} {:height 150}])

— Reply to this email directly, view it on GitHub https://github.com/metosin/malli/issues/936#issuecomment-1691450857, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFZDB3EFMQ7XU3QNH44MULXW4WWDANCNFSM6AAAAAA34PJC24 . You are receiving this because you authored the thread.Message ID: @.***>

knubie commented 7 months ago

I think this is related to / dup of #764

allentiak commented 5 months ago

It would be nice if something like this could work. For this, named params are awesome.

(defn area-of-a-square
[len]
{:malli/schema [:=> [:catn [:len nat-int?]]
     [:catn [:area
              [:and
               nat-int?
               [:fn (fn [{:keys [area len]}]
                  (>= area len))]]]]]}
(* len len))

With clojure.spec, I would write it in this way:

;; len --> area
(defn area-of-square
  [len]
  (* len len))

(s/fdef area-of-square
  :args (s/cat :len nat-int?)
  :ret nat-int?
  :fn (fn [{:keys [ret len]}]
        (>= ret len))]]]]]}
ikitommi commented 5 months ago

quick poke on this.

Screenshot 2024-01-24 at 19 00 01
ikitommi commented 5 months ago

fixed in #1000.