Skip to content

Commit

Permalink
Merge pull request #992 from metosin/nail-polish
Browse files Browse the repository at this point in the history
Small Improvements
  • Loading branch information
ikitommi authored Jan 11, 2024
2 parents 2aac49a + 48adf56 commit 029925c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 1 addition & 2 deletions docs/reusable-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ You should pick the way what works best for your project.
# Future Work

1. Could we also decomplect the Maps, Keys and Values with the Var Style?
2. Clear separation of [Entities and Values](https://martinfowler.com/bliki/EvansClassification.html)
3. Utilities for transforming between inlined and referenced models (why? why not!)
2. Utilities for transforming between inlined and referenced models (why? why not!)

```clojure
(-flatten-refs
Expand Down
10 changes: 7 additions & 3 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@
(if (-equals properties (-properties schema))
schema (-into-schema (-parent schema) properties (or (and (-entry-schema? schema) (-entry-parser schema)) (-children schema)) (-options schema))))

(defn -update-properties [schema f & args]
(-set-properties schema (not-empty (apply f (-properties schema) args))))

(defn -update-options [schema f]
(-into-schema (-parent schema) (-properties schema) (-children schema) (f (-options schema))))

Expand Down Expand Up @@ -2310,12 +2313,13 @@
"Derefs all schemas at all levels. Does not walk over `:ref`s."
([?schema]
(deref-recursive ?schema nil))
([?schema options]
(let [schema (schema ?schema options)]
([?schema {::keys [ref-key] :as options}]
(let [schema (schema ?schema options)
maybe-set-ref (fn [s r] (if (and ref-key r) (-update-properties s assoc ref-key r) s))]
(-> (walk schema (fn [schema _ children _]
(cond
(= :ref (type schema)) schema
(-ref-schema? schema) (first children)
(-ref-schema? schema) (-> children (first) (maybe-set-ref (-ref schema)))
:else (-set-children schema children)))
{::walk-schema-refs true})
(deref-all)))))
Expand Down
2 changes: 1 addition & 1 deletion src/malli/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"Returns a Schema instance with updated properties."
[?schema f & args]
(let [schema (m/schema ?schema)]
(m/-set-properties schema (not-empty (apply f (m/-properties schema) args)))))
(apply m/-update-properties schema f args)))

(defn closed-schema
"Maps are implicitly open by default. They can be explicitly closed or
Expand Down
27 changes: 18 additions & 9 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3052,12 +3052,21 @@
[:name :string]
[:friends {:optional true} [:set [:ref ::user]]]
[:address ::address]]}}
::user]
expected [:map
[:id :uuid]
[:name :string]
[:friends {:optional true} [:set [:ref ::user]]]
[:address [:map
[:street :string]
[:lonlat {:optional true} [:tuple :double :double]]]]]]
(is (= expected (m/form (m/deref-recursive schema))))))
::user]]
(is (= [:map
[:id :uuid]
[:name :string]
[:friends {:optional true} [:set [:ref ::user]]]
[:address [:map
[:street :string]
[:lonlat {:optional true} [:tuple :double :double]]]]]
(m/form (m/deref-recursive schema))
(m/form (m/deref-recursive schema {::m/ref-key nil}))))
(is (= [:map {:id ::user}
[:id [:uuid {:id ::user-id}]]
[:name :string]
[:friends {:optional true} [:set [:ref ::user]]]
[:address [:map {:id ::address}
[:street :string]
[:lonlat {:optional true} [:tuple :double :double]]]]]
(m/form (m/deref-recursive schema {::m/ref-key :id}))))))

0 comments on commit 029925c

Please sign in to comment.