Fork me on GitHub
#malli
<
2022-04-15
>
hjrnunes18:04:59

Hi. Say I want to validate a sequence of maps and make sure there's at least one of them with a specific entry. Is this in scope for Malli? How would I go about it? Example:

;; my schema is something that somehow looks for [:b 2] in a map sequence

(m/validate my-schema [{:a 1} {:b 2} {:c 3}])
=> true

(m/validate my-schema [{:a 1} {:b 2} {:b 2 :c 4} {:c 3}])
=> true

(m/validate my-schema [{:a 1} {:c 3}])
=> false

🙌 1
Noah Bogart19:04:16

you can use https://cljdoc.org/d/metosin/malli/0.8.4/doc/readme#fn-schemas to validate "anything":

(def my-schema
  [:and
   [:sequence map?]
   [:fn (fn [s] (some #(= 2 (:b %)) s))]])
i haven't tried that but it should work

Luke Johnson20:04:58

I’m sure this has been asked before, but is there a way to generate a value compared to the generated value of a key in the same map schema?

{:min-value 250  ;; generated
 :max-value 350} ;; always exactly 100 more than min
Looking through the documentation, I can’t find anything specific. https://github.com/metosin/malli/blob/master/docs/tips.md#dependent-string-schemas looks promising or maybe using :gen/fmap but I could really use some guidance.