Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore optional schemas when generating default values #351

Closed
nivekuil opened this issue Jan 26, 2021 · 1 comment · Fixed by #397
Closed

Ignore optional schemas when generating default values #351

nivekuil opened this issue Jan 26, 2021 · 1 comment · Fixed by #397
Labels
enhancement New feature or request

Comments

@nivekuil
Copy link

nivekuil commented Jan 26, 2021

default-value-transformer is handy, but I want it to ignore schemas with an {:optional true} property in the outputted value. I made a pass at writing a variant that does so:

(defn default-value-optional-transformer
  ([]
   (default-value-optional-transformer nil))
  ([{:keys [key defaults] :or {key :default}}]
   (let [get-default  (fn [schema] (let [properties (m/properties schema)]
                                   (when-not (:optional properties)
                                     (if-some [default (get properties key)]
                                       default
                                       (some->> schema m/type (get defaults) (#(% schema)))))))
         set-default  {:compile (fn [schema _]
                                  (when-some [default (get-default schema)]
                                    (fn [x] (if (nil? x) default x))))}
         add-defaults {:compile (fn [schema _]
                                  (let [defaults (into {}
                                                       (keep (fn [[k {default key optional :optional} v]]
                                                               (when-not optional
                                                                 (when-some [default (if (some? default) default (get-default v))]
                                                                   [k default]))))
                                                       (m/children schema))]
                                    (when (seq defaults)
                                      (fn [x]
                                        (if (map? x)
                                          (reduce-kv
                                           (fn [acc k v]
                                             (if (contains? x k)
                                               acc
                                               (assoc acc k v)))
                                           x defaults)
                                          x)))))}]
     (transformer
      {:default-decoder set-default
       :default-encoder set-default}
      {:decoders {:map add-defaults}
       :encoders {:map add-defaults}}))))

Not sure if this is behavior is better as a separate fn or an option in default-value-transformer.

@ikitommi
Copy link
Member

I think we should ignore optional keys by default. PR welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants