I made a PR to malli which makes sci optional. The tests are failing, but this is only because the order in which namespaces are loaded is random. sci.core needs to be loaded first. But since sci.core is now optional, the tests have to be refactored accordingly anyway. Just putting this here in case someone has ideas about it.
Locally the tests pass, FWIW.
I might put that dynaload code into a library so it can be used in more projects.
will check that out, thanks!
a separat one-purpose lib would be good
merged recursion PR into master.
Hi. I have an use case with the data structure and scenario similar to the following example
{
"name": string,
"age": int,
"married": boolean,
"sex": string
"partner": {
"husband":{"name": string, "age": int},
"wife":{"name": string, "age": int}
}
}
name in husband is mandatory if the status is married and sex is Male. name in wife is mandatory if status is married and sex is female. both are not needed if married is false. how do i write the malli schema validations for the following scenario. if both the fields are at the same level i could add a function and validation. since one is at a parent and the other field is inside the child, i am unable to find a solution to implement this. any help would be appreciated. thanks in advance
hi @kspriyan31. You can add rules to top-level and add error metadata to point into nested fields. Something like:
(def Person
[:and {:registry {:partner [:map ["name" string?] ["age" int?]]}}
[:map
["name" string?]
["age" int?]
["married" boolean?]
["sex" string?]
["partner" {:optional true}
[:map
["husband" {:optional true} :partner]
["wife" {:optional true} :partner]]]]
[:fn {:error/message "wife name is mandatory for married men"
:error/path ["partner" "wife" "name"]}
(fn [{:strs [sex married partner]}]
(not (and married (= "male" sex) (not (get-in partner ["wife" "name"])))))]
[:fn {:error/message "husband name is mandatory for married female"
:error/path ["partner" "wife" "name"]}
(fn [{:strs [sex married partner]}]
(not (and married (= "female" sex) (not (get-in partner ["husband" "name"])))))]])
(-> Person
(m/explain {"name" "Mauno"
"age" 31
"married" true
"sex" "male"
"partner" {"wife" {"age" 41}}})
(me/humanize))
;{"partner" {"wife" {"name" ["missing required key"
; "wife name is mandatory for married men"]}}}
here’s the same in http://malli.io: http://shorturl.at/hmuFV
Thank you @ikitommi
if you change the sex
to [:enum "male" "female" "other"]
the sample value generation can generate better values:
({"name" "",
"age" 1,
"married" true,
"sex" "male",
"partner" {"husband" {"name" "", "age" -1}, "wife" {"name" "", "age" 0}}}
{"name" "", "age" 2, "married" false, "sex" "male"}
{"name" "K5", "age" 2, "married" false, "sex" "male", "partner" {"wife" {"name" "", "age" -1}}}
{"name" "", "age" 3, "married" false, "sex" "male"}
{"name" "8Mn",
"age" 3,
"married" true,
"sex" "male",
"partner" {"husband" {"name" "J", "age" -4}, "wife" {"name" "", "age" 3}}}
{"name" "bsyt4", "age" 1, "married" false, "sex" "female"}
{"name" "8k5yk", "age" 6, "married" false, "sex" "female", "partner" {"husband" {"name" "4", "age" 13}}}
{"name" "5ut0D2sm",
"age" 1,
"married" true,
"sex" "female",
"partner" {"husband" {"name" "", "age" -102}, "wife" {"name" "i4Q5l6xa", "age" 0}}}
{"name" "", "age" 1, "married" false, "sex" "male"}
{"name" "l", "age" 3, "married" false, "sex" "male", "partner" {"husband" {"name" "Ixh4drJNb", "age" -37}}})
your welcome