Skip to content

Commit

Permalink
Merge pull request #953 from jasonjckn/assert
Browse files Browse the repository at this point in the history
feat(assert): adds new assert-like functionality to check malli schema
  • Loading branch information
ikitommi authored Oct 31, 2023
2 parents 8a864a3 + 72c4346 commit ab76f78
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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}))))

0 comments on commit ab76f78

Please sign in to comment.