Fork me on GitHub
#malli
<
2021-01-29
>
danielneal10:01:41

what order do :and schemas operate in

danielneal10:01:53

is it possible to do

(m/explain [:and
              int?
              (m/-simple-schema
                {:pred #(> % 3)})] "asdf")

danielneal10:01:30

and have the int? check prevent the pred from running and erroring?

danielneal11:01:53

like in spec

(s/def ::greater-than-3
    (s/and int? #(> % 3)))

dharrigan11:01:23

This will work...

dharrigan11:01:30

(def foo [:and int? [:fn (fn [x] (> x 3))]])

dharrigan11:01:39

#'user/foo
user=> (m/validate foo "abcd")
false
user=> (m/validate foo 1)
false
user=> (m/validate foo 3)
false
user=> (m/validate foo 4)
true

dharrigan11:01:41

You can, of course, shorten it, i.e., (def foo [:and int? [:fn #(> % 3)]])

danielneal11:01:03

interesting, I wonder why :fn works but -simple-schema doesn't

dharrigan11:01:00

It's in the documentation to use that 🙂

dharrigan11:01:06

You can try it out here as well

danielneal11:01:46

cool, I'll use :fn 🙂

dharrigan11:01:56

Actually, it works with simple-schema too

dharrigan11:01:09

user=> (def foo2 [:and int? (m/-simple-schema {:pred #(> % 3)})])
#'user/foo2
user=> (m/validate foo2 4)
true
user=> (m/validate foo2 3)
false
user=> (m/validate foo2 1)
false
user=> (m/validate foo2 "and")
false
user=> 

dharrigan11:01:39

I missed that, so I suppose you can use that as well 🙂

dharrigan11:01:22

choices...choices...

danielneal11:01:14

@dharrigan, validate succeeds, but explain fails

danielneal11:01:20

could be a bug perhaps?

danielneal11:01:39

(m/explain foo2 "and") => java.lang.ClassCastException

danielneal12:01:24

I guess it does makes sense that explain on an :and would give all the reasons something fails, but it doesn't make sense -simple-schema fails where :fn doesn't.

juhoteperi12:01:32

If you are just checking > you could also use [:int {:min 3}] or [:and int? [:>= 3]].

👍 1
juhoteperi12:01:46

First one works best with JSON Schema

danielneal12:01:25

ah, the actual example here is a bit different, it's for a kebab case keyword

danielneal12:01:45

[:and
    {:description "A keyword joined by hyphens"
     :swagger/description "A string joined by hyphens"
     :swagger/example "kebab-case-string"
     :error/message "should be a kebab-case keyword"}
     keyword?
    [:fn kebab-case-keyword?]]

danielneal12:01:36

(where in this case kebab-case-keyword? fails on non-keywords)

ikitommi15:01:42

WIP: Schemas of Schemas:

(-> (m/-simple-schema
      {:type :int
       :pred string?
       :properties-schema [:map [:min nat-int?] [:max nat-int?]]
       :property-pred (m/-min-max-pred nil)})
    (m/properties-schema))
; => [:map [:min nat-int?] [:max nat-int?]]
also:
(let [OneOf (m/-simple-schema
              (fn [{:keys [count]} values]
                (let [value? (set values)]
                  {:type :user/over
                   :pred value?
                   :properties-schema [:map [:count [:= count]]]
                   :children-schema (into [:tuple] (map (fn [x] [:= x]) values))
                   :type-properties {:error/message (str "should be one of " values)
                                     :json-schema {:type "oneOf", :values values}}})))
      schema (m/schema [OneOf {:count 6} 1 2 3 4 5 6])]
  {:properties-schema (m/properties-schema schema)
   :children-schema (m/children-schema schema)})
;{:properties-schema [:map [:count [:= 6]]]
; :children-schema [:tuple [:= 1] [:= 2] [:= 3] [:= 4] [:= 5] [:= 6]]}