How would this content dependent schema example go with a string property passed for compile? I always get :malli.core/child-error Below my last attempt, I stripped off a lot of keywords, but regardless if they are present or not, the result is always the same. What is missing from here?
(def EOkind
(m/-simple-schema
{:type `EOkind
:compile (fn [_properties [kind] _options]
{:pred #(= (:kind %) kind)
:type-properties {:error/fn (fn [error _] (str "Kind should be " kind " , was " (:value error)))
:decode/string mt/string-transformer}})}))
user=> (m/validate [EOkind "program"] {:id 2 :kind "program"})
Execution error (ExceptionInfo) at malli.core/-exception (core.cljc:138).
:malli.core/child-erroryou need the :min and :max to tell malli what the minimum and maximum number of children is. the defaults are 0
user=> (def EOkind
(m/-simple-schema
{:type `EOkind
:compile (fn [_properties [kind] _options]
{:pred #(= (:kind %) kind)
:min 1
:max 1
:type-properties {:error/fn (fn [error _] (str "Kind should be " kind " , was " (:value error)))
:decode/string mt/string-transformer}})}))
#'user/EOkind
user=> (m/validate [EOkind "program"] {:id 2 :kind "program"})
true
user=> (m/validate [EOkind "program"] {:id 2 :kind "foo"})
false
this works for me!if you don't have child schemas (something like [EOkind [:map [:foo :string]]] but just parameters to the schema, you can use the :property-pred functionality of -simple-schema like this:
user=> (def EOkind
(m/-simple-schema
{:type `EOkind
:pred #(map? %)
:property-pred (fn [props]
(fn [value]
(= (:kind value) (:kind props))))}))
#'user/EOkind
user=> (m/validate [EOkind {:kind "program"}] {:id 2 :kind "program"})
true
user=> (m/validate [EOkind {:kind "program"}] {:id 2 :kind "foo"})
falseyou can still use :type-properties without :compile if you want to set the error-fn etc
Ahh, ok. Thank you again! 🙂