What’s the difference between encode/decode parse/unparse?
I see encode as a more general parse, in that you could probably implement parse in terms of encode.
Maybe you'd need to combine it with validate.
parse is useful if you want more information about why a value is valid. It's sort of the dual of m/explain, which gives information about why value is invalid.
encode is useful for transforming values into other formats and back.
encode is very powerful in comparison to parse. If you want to do something other than just figure out which :orn branch was picked to validate a value, you probably want to look into encode.
(m/parse [:orn [:left :any] [:right :any]] :asdf)
;=> #malli.core.Tag{:key :left, :value :asdf}
(m/unparse [:orn [:left :any] [:right :any]] #malli.core.Tag{:key :left, :value :asdf})
;=> :asdf
Thanks
i feel silly asking this, but what does the :size flag do when using mg/generate?
size is an internal test.check value that is used to control the "size" of generators. This means different things for different generators. it starts small and increases as the test demands larger input values. Maybe shrinking uses it to reverse the process, but I don't know how shrinking works.
got you, makes sense then why i usually saw it used most-often along-side :seed in the malli README
ah, I tracked it down to clojure.test.check.generators, but uh, this does not clear much up for me
Well, I ask because I'm trying to generate a spec defined as a [:sequential <something>], and I'd like to be able to explicitly generate something matching that spec with X elements
was hoping for something like (mg/generate [:sequential int?] {:magical-size-option 4}) => [1 3 -2 67]
[:sequential {:gen/min 4 :gen/max 4} int?]
ahhhhhhhhh
also better for the ad-hoc update-properties
i'd prefer to not hard-code the generated size, as I'm trying to restrict the size so that it matches the count of another variably sized sequence
You may need to write your own generator for the outer-most thing and use gen/fmap.
Please provide more details if you can.
this behaves perfectly for my use-case
(defn sized-gen [spec size]
(mg/generate (mu/update-properties spec assoc :gen/min size :gen/max size)))
which i would use as
(sized-gen <my-spec> (count <some-sequence>))(function-name certainly not final), im just using this for futzing around in a devfile
appreciate the info otherwise! o/
I might be late to the party but I think you could use tuple for this, too
e.g. (mg/generate [:tuple int? int? int?])
(defn sized-gen [mschema n]
(mg/generate (into [:tuple] (repeat n mschema))))
(sized-gen :string 2)
;; => ["90S1" "G7G3j1a9O61l89U3rXj1rstbTY9"]🤷♂️
Well the goal was that I could take an existing :sequential spec, and purely for testing purposes generate a sequence that otherwise matches the spec, but only contains X elements
(I would rarely be using [:sequential ...] verbatim here, and would really just be referencing an existing spec in another namespace)
hack, but:
(first (mc/children [:sequential {:min 5} :string]))
;; => :stringI think that ambrose's answer is good
this is the best i've got so far
(mg/generate (mu/update-properties [:sequential int?] assoc :min 4 :max 4))