malli

https://github.com/metosin/malli :malli:
danielneal 2021-01-29T10:43:41.031600Z

what order do :and schemas operate in

danielneal 2021-01-29T10:43:53.031900Z

is it possible to do

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

danielneal 2021-01-29T10:44:30.032200Z

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

danielneal 2021-01-29T11:02:53.032600Z

like in spec

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

dharrigan 2021-01-29T11:13:23.032800Z

This will work...

dharrigan 2021-01-29T11:13:30.033Z

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

dharrigan 2021-01-29T11:13:39.033200Z

#'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

dharrigan 2021-01-29T11:16:41.033500Z

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

danielneal 2021-01-29T11:18:03.033900Z

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

dharrigan 2021-01-29T11:19:00.034100Z

It's in the documentation to use that 🙂

dharrigan 2021-01-29T11:19:06.034300Z

You can try it out here as well <https://malli.io/?value=1&amp;schema=%5B%3Aand%20int%3F%20%5B%3Afn%20(fn%20%5Bx%5D%20(%3E%20x%203))%5D%5D>

danielneal 2021-01-29T11:26:46.034600Z

cool, I'll use :fn 🙂

dharrigan 2021-01-29T11:29:56.034900Z

Actually, it works with simple-schema too

dharrigan 2021-01-29T11:30:09.035100Z

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

dharrigan 2021-01-29T11:30:39.035400Z

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

dharrigan 2021-01-29T11:31:22.035700Z

choices...choices...

danielneal 2021-01-29T11:41:14.036Z

@dharrigan, validate succeeds, but explain fails

danielneal 2021-01-29T11:41:20.036200Z

could be a bug perhaps?

danielneal 2021-01-29T11:41:39.036600Z

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

danielneal 2021-01-29T12:08:24.037700Z

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.

juhoteperi 2021-01-29T12:09:32.038400Z

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

👍 1
juhoteperi 2021-01-29T12:09:46.038600Z

First one works best with JSON Schema

danielneal 2021-01-29T12:12:25.039200Z

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

danielneal 2021-01-29T12:13:45.039500Z

[: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?]]

danielneal 2021-01-29T12:14:36.040200Z

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

ikitommi 2021-01-29T15:08:42.040900Z

WIP: Schemas of Schemas:

(-&gt; (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))
; =&gt; [: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]]}