Fork me on GitHub
#malli
<
2022-05-16
>
vemv11:05:32

I might need a refresher. Given data expressed as strings (because the data comes from something that inherently only expresses things as strings e.g. a spreadsheet or csv) and a richer schema (i.e. attribute x is an int, not a string), how do I coerce those string values into a schema? Example:

(my/coerce [:sequential [:map [:a int?]]]
           [{:a "1"}]) ;; => Produces `[{:a 1}]`, i.e. coerces the string into an int
;; ...and throws if no coercion was possible

ikitommi11:05:08

it does not throw by default, but you can validate the result and throw if it's invalid. Two walks on the schema with malli are still much faster than one (decode or throw) with most others.

1
vemv11:05:39

thanks much! Let's give it a go :)

chen florescu13:05:50

(def example-schema
  [:map {:closed true}
   [:product {:optional true} Product]
   [:version {:default "1.0"} [:= "1.0"]]
   [:enable true?]
   [:window [:int {:min 12 :max 1512}]]])
I have this schema and I want to add a constraint that only if I receive product (which is optional), then window needs to be [:int {:min 12 :max 24}]. Any suggestions how can I do that?

Ben Sless15:05:57

Those are two different schemas and there's an or between them. Then product won't be option, either