eval / malli-select

spec2-inspired selection of Malli-schemas
MIT License
32 stars 0 forks source link

more useful generators #8

Open eval opened 10 months ago

eval commented 10 months ago

Currently selecting is helpful to create tailored schemas:

(ms/select Person ^:only [:name :age {:country [:iso]}])

But when used to generate data you often may want to set the properties of some attributes to make it an actually helpful generator for the test/context you'r working on. So:

;; we'd want to turn this...
[:map [:name string?] [:age pos-int?] :country [:map [:iso string?]]]

;; ...into this:
[:map 
  [:name {:gen/return "Gert"} string?]
  [:age [:int {:min 18 :max 50}]]
  [:country
    [:map 
      [:iso {:gen/elements ["NL" "DK"]} string?]]]]

Sketching:

(-> Person
  (ms/select ^:only [:name :age {:country [:iso]}])
  (ms/merge-in-properties [:name] {:gen/return "Gert"})
  (ms/merge-in-properties [:age] {:min 18 :max 50})
  (ms/merge-in-properties [:country iso] {:gen/elements ["NL" "DK"]})
  (mg/generate)) ;; => {:name "Gert" :age 21 :country "DK"}

or even:

(-> Person
  (ms/select ^:only [:name :age {:country [:iso]}])
  (ms/merge-in-properties [[:name {:gen/return "Gert"}]
                           [:age {:min 18 :max 50}]
                           [:country [:iso {:gen/elements ["NL" "DK"]}]]])
  (mg/generate)) ;; => {:name "Gert" :age 21 :country "DK"}

Upvote & Fund

Fund with Polar