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

:m/default for :multi #391

Merged
merged 2 commits into from Mar 13, 2021
Merged

:m/default for :multi #391

merged 2 commits into from Mar 13, 2021

Conversation

ikitommi
Copy link
Member

Default branch with :m/default:

(def valid?
  (m/validator
    [:multi {:dispatch :type}
     ["object" [:map-of :keyword :string]]
     [:m/default :string]]))

(valid? {:type "object", :key "1", :value "100"})
; => true

(valid? "SUCCESS!")
; => true

(valid? :failure)
; => false

Copy link

@ilmoraunio ilmoraunio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@@ -1087,7 +1087,8 @@
{:keys [children entries forms]} (-parse-entries children opts' options)
form (-create-form type properties forms)
dispatch (eval (:dispatch properties) options)
dispatch-map (->> (for [[k s] entries] [k s]) (into {}))]
dispatch-map (->> (for [[k s] entries] [k s]) (into {}))
finder (fn [{:keys [:m/default] :as m}] (fn [x] (m x default)))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[aside] TIL you can destructure & reuse keys like that.

(m/form schema)))))
(m/form schema))))

(let [schema [:multi {:dispatch first}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Could test that dispatching :m/default works both with {:dispatch :type} and {:dispatch first}.

@DerGuteMoritz
Copy link
Contributor

I vote for using :malli.core/default / ::m/default.

@ilmoraunio
Copy link

Good catch. Looks like grouped keywords can be too opinionating especially going forward with such a highly common prefix like m.

@DerGuteMoritz
Copy link
Contributor

Exactly. Also, there even is some prior use of that in Malli itself already with the various options for m/walk 🙂

@ikitommi
Copy link
Member Author

::m/default it is then. for humanized errors, the top-level key is :malli/error, which should be changed too, but a breaking change in the public api we can postpone. thanks for the comments.

@ikitommi ikitommi merged commit 9777a64 into master Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants