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

Allow qualified symbols as references #984

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Malli is in well matured [alpha](README.md#alpha).
* does not log individual re-instrumentation of function vars
* **BREAKING**: changes in `malli.dev.virhe` and `malli.pretty` extension apis, wee [#980](https://github.com/metosin/malli/pull/980) for details
* **BREAKING**: `m/coerce` and `m/coercer` throw `::m/coercion` instead of `::m/invalid-input`
* **BREAKING**: qualified symbols are valid reference types [#984](https://github.com/metosin/malli/pull/984)
* Fixing `mt/strip-extra-keys-transformer` for recursive map encoding [#963](https://github.com/metosin/malli/pull/963)
* Support passing custom `:type` in into-schema opt for `:map` and `:map-of` [#968](https://github.com/metosin/malli/pull/968)
* `mu/path->in` works with `:orn`, `:catn` and `:altn`.
Expand Down
2 changes: 1 addition & 1 deletion src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@

(defn -pointer [id schema options] (-into-schema (-schema-schema {:id id}) nil [schema] options))

(defn -reference? [?schema] (or (string? ?schema) (qualified-keyword? ?schema)))
(defn -reference? [?schema] (or (string? ?schema) (qualified-ident? ?schema)))

(defn -lazy [ref options] (-into-schema (-ref-schema {:lazy true}) nil [ref] options))

Expand Down
20 changes: 10 additions & 10 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1948,21 +1948,21 @@
options))))))

(deftest custom-registry-qualified-keyword-in-map-test
(let [schema [:map {:registry {::id int?
::location [:tuple :int :int]
::country string?}}
::id
[::location]
(let [schema [:map {:registry {:user/id int?
"user/location" [:tuple :int :int]
'user/country string?}}
:user/id
["user/location"]
[:name string?]
[::country {:optional true}]]]
['user/country {:optional true}]]]

(testing "Example with qualified keyword + optional, regular key"
(is (m/validate schema {::id 123 ::location [1 1] ::country "Finland" :name "Malli"})))
(testing "Example with references + optional, regular key"
(is (m/validate schema {:user/id 123 "user/location" [1 1] 'user/country "Finland" :name "Malli"})))

(testing "Optional qualified keyword is optional"
(is (m/validate schema {::id 123 ::location [1 1] :name "Malli"}))))
(is (m/validate schema {:user/id 123 "user/location" [1 1] :name "Malli"}))))

(testing "invalid ref"
(testing "invalid entry"
(is (thrown-with-msg?
#?(:clj Exception, :cljs js/Error) #":malli.core/invalid-entry"
(m/schema [:map {:registry {:kikka :int}} :int])))))
Expand Down