what order do :and
schemas operate in
is it possible to do
(m/explain [:and
int?
(m/-simple-schema
{:pred #(> % 3)})] "asdf")
and have the int?
check prevent the pred from running and erroring?
like in spec
(s/def ::greater-than-3
(s/and int? #(> % 3)))
This will work...
(def foo [:and int? [:fn (fn [x] (> x 3))]])
#'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
You can, of course, shorten it, i.e., (def foo [:and int? [:fn #(> % 3)]])
interesting, I wonder why :fn
works but -simple-schema
doesn't
It's in the documentation to use that 🙂
You can try it out here as well <https://malli.io/?value=1&schema=%5B%3Aand%20int%3F%20%5B%3Afn%20(fn%20%5Bx%5D%20(%3E%20x%203))%5D%5D>
cool, I'll use :fn
🙂
Actually, it works with simple-schema too
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=>
I missed that, so I suppose you can use that as well 🙂
choices...choices...
@dharrigan, validate succeeds, but explain fails
could be a bug perhaps?
(m/explain foo2 "and")
=> java.lang.ClassCastException
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.
If you are just checking >
you could also use [:int {:min 3}]
or [:and int? [:>= 3]]
.
First one works best with JSON Schema
ah, the actual example here is a bit different, it's for a kebab case keyword
[: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?]]
(where in this case kebab-case-keyword? fails on non-keywords)
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]]}