metosin / malli

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

Validate :or on map keys #935

Closed blindcoding9 closed 10 months ago

blindcoding9 commented 10 months ago

How can I validate that the map has one key or another?

(m/validate [:map
               [:or :A :B]]
              {:A "S"})
(m/validate [:map
               [:or [:A :string] [:B :string]]]
              {:A "S"})

Both:

=> Execution error (ExceptionInfo) at malli.core/-exception (core.cljc:138).
:malli.core/invalid-schema
dainiusjocas commented 10 months ago

This trick works:

(let [schema [:or 
              [:map {:closed true}
               [:A :string]]
              [:map {:closed true}
               [:B :string]]]]
  [(m/validate schema {})
   (m/validate schema {:A "S"})
   (m/validate schema {:B "B"})
   (m/validate schema {:A "S" :B "B"})])
=> [false true true false]
ikitommi commented 10 months ago

duplicate of #474