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

feat(assert): adds new assert-like functionality to check malli schema #953

Merged
merged 1 commit into from
Oct 31, 2023
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
14 changes: 13 additions & 1 deletion src/malli/core.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns malli.core
(:refer-clojure :exclude [eval type -deref deref -lookup -key])
(:refer-clojure :exclude [eval type -deref deref -lookup -key assert])
#?(:cljs (:require-macros malli.core))
(:require #?(:clj [clojure.walk :as walk])
[clojure.core :as c]
Expand Down Expand Up @@ -2230,6 +2230,18 @@
([?schema value transformer respond raise] (coerce ?schema value transformer respond raise nil))
([?schema value transformer respond raise options] ((coercer ?schema transformer respond raise options) value)))

(defmacro assert
"Assert that `value` validates against schema `?schema`, or throws ExceptionInfo.
The var clojure.core/*assert* determines whether assertion are checked."

([?schema value]
`(assert ~?schema ~value nil))

([?schema value options]
(if *assert*
`(coerce ~?schema ~value nil ~options)
value)))

(defn entries
"Returns `EntrySchema` children as a sequence of `clojure.lang/MapEntry`s
where the values child schemas wrapped in `:malli.core/val` Schemas,
Expand Down
4 changes: 3 additions & 1 deletion src/malli/dev/virhe.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
(let [colors (:colors printer -dark-colors)
color (get colors color (:error colors))]
#?(:cljs [:span body]
:clj [:span [:pass (str "\033[38;5;" color "m")] body [:pass "\u001B[0m"]])))
:clj (if color
[:span [:pass (str "\033[38;5;" color "m")] body [:pass "\u001B[0m"]]
[:span body]))))

;;
;; EDN
Expand Down
29 changes: 29 additions & 0 deletions test/malli/assert_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns malli.assert-test
(:refer-clojure :exclude [assert])
(:require
[clojure.test :refer [deftest is]]
[malli.core :refer [assert]]))


(set! *assert* true)

(deftest assert-throws-test
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert :int "42" )))
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert int? "42" )))
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert string? 42)))
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert int? nil)))
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert [:map [:a int?]] {:a "42"})))
(is (thrown? #?(:clj Exception, :cljs js/Error)
(assert ::invalid-schema 42))))

(deftest assert-checked-and-does-not-throw
(is (= 42 (assert :int 42 )))
(is (= 42 (assert int? 42 )))
(is (= "42" (assert string? "42")))
(is (= nil (assert any? nil)))
(is (= {:a 42} (assert [:map [:a int?]] {:a 42}))))