Hey all. I might be missing something essential here but I don’t fundamentally understand why this won’t compile:
(defn longer-than-5? [x] (<= 5 (count x)))
(def MySchema [:map [:some-key [:and string? longer-than-5?]]])
; :malli.core/invalid-schema {:schema #function[example/longer-than-5?]}
Is there something special about string?
, int?
ect that makes them more than just a function from a -> bool
?What is special is that they are in the m/predicate-schemas
registry which is merged into the default registry
@nilern That’s interesting. Thank you
Looks like I need to specify the function with [:fn longer-than-5?]
!
Hi,
I think the strip-extra-keys-transformer
removes mandatory fields if there are in an [:or ... ]
vector:
(def my-schema [:and
[:map [:a int?]]
[:or
[:map [:b int?]]
[:map [:c int?]]]])
=> #'...
(m/validate my-schema {:a 1})
=> false
(m/validate my-schema {:a 1 :b 1 :c 1})
=> true
(m/decode my-schema {:a 1 :b 1 :c 1} (mt/transformer mt/strip-extra-keys-transformer))
=> {:a 1}
Did I configure the schema wrong?
How can I work around it?maybe:
[:and
[:map
[:a int?]
[:b {:optional true} int?]
[:c {:optional true} int?]]
[:fn {:error/messag "only :a or :b is allowed"}
(fn [{:keys [b c]}] (not (and b c)))]]
or:
[:or
[:map
[:a int?]
[:b int?]]
[:map
[:a int?]
[:c int?]]]
would those work for you?Checking, I want to see how they integrate with reitit
’s swagger docs
you can run from repl:
(malli.swagger/transform
[:and
[:map
[:a int?]
[:b {:optional true} int?]
[:c {:optional true} int?]]
[:fn {:error/messag "only :a or :b is allowed"}
(fn [{:keys [b c]}] (not (and b c)))]])
to see what comes out.swagger doesn’t support anyOf
JSON Schema, so the latter will not show in swaggr docs.
openapi has that, but not integrated into reiti.
Hi @ikitommi I am facing interesting behavior with explain. I don't understand why order matters...
(def params-function?
(malli/schema
[:function
[:=> [:cat map? [:maybe map?] map?] map?]]
{::malli/function-checker malli.generator/function-checker}))
(def next-function?
(malli/schema
[:function
[:=> [:cat map? [:maybe map?] map?] keyword?]]
{::malli/function-checker malli.generator/function-checker}))
I’ve used the first option, it worked for me. Thanks!
@dos oh, that’s bad. will fix it.
Fixed in master: https://github.com/metosin/malli/pull/421