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

Adds :pred option to m/-map-schema #767

Merged
merged 6 commits into from Oct 18, 2022
Merged

Adds :pred option to m/-map-schema #767

merged 6 commits into from Oct 18, 2022

Conversation

fr33m0nk
Copy link
Contributor

@fr33m0nk fr33m0nk commented Oct 17, 2022

Context:

  • Add ability to validate custom map-like datatype e.g. datomic.query.EntityMap
  • This PR is product of @ikitommi suggestion on slack thread
;;Would it help if the malli.core/-map-schema took an extra option for the predicate? e.g. on could just do:

(def EntityMap (m/-map-schema {:pred entity?}))

(m/validate EntityMap ...)
  • :pred key-value is optional, just like :lazy and :naked-keys. In absence of explicit value, map? is used as default predicate (referred to as pred? in code)

Usage with PR:

;; regular usage still works just fine
(m/validate
  (m/schema [:map
             [:member/id uuid?]
             [:member/organization [:map
                                    [:organization/id uuid?]]]])
  {:db/id 409018325533822,
   :member/id #uuid"ff995e8b-77c0-4c03-a8a8-c5db609a2f16",
   :member/organization {:db/id 17592186045562, :organization/id #uuid"25dd7d64-6f83-49fe-bd36-253e985b3e00"}})
=> true

;; a custom predicate fn
(defn entity?
  [entity]
  (instance? datomic.query.EntityMap entity))

;; example member entity
member-entity
=>
{:db/id 409018325533822,
 :member/id #uuid"ff995e8b-77c0-4c03-a8a8-c5db609a2f16",
 :member/organization {:db/id 17592186045562, :organization/id #uuid"25dd7d64-6f83-49fe-bd36-253e985b3e00"}}

;; validation of entity

(m/validate
  (m/schema [:entity-map
             [:member/id uuid?]
             [:member/organization [:entity-map
                                    [:organization/id uuid?]]]]
            {:registry (assoc (m/default-schemas) :entity-map (m/-map-schema {:pred entity?}))})
  mmm)
=> true

@fr33m0nk fr33m0nk changed the title [fr33m0nk]: Adds :pred option to m/-map-schema Adds :pred option to m/-map-schema Oct 17, 2022
Copy link
Member

@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

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

One small comment + could you add a simple test of this too, to avoid future regression. Thanks!

src/malli/core.cljc Outdated Show resolved Hide resolved
Copy link
Member

@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

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

Thanks, looks great!

@ikitommi ikitommi merged commit 6f1a986 into metosin:master Oct 18, 2022
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

2 participants