-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assert): adds new assert-like functionality to check malli schema
- Loading branch information
Showing
3 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns malli.assert | ||
(:refer-clojure :exclude [assert]) | ||
(:require | ||
[malli.core :as m] | ||
[malli.dev.pretty :as pretty] | ||
[malli.util :as mu])) | ||
|
||
(def ^:dynamic *reporter* (pretty/thrower (pretty/-printer {:colors nil}))) | ||
|
||
|
||
(defmacro assert | ||
"Assert that `x` validates against schema `?schema`, or throws ExceptionInfo. | ||
The clojure.core/*assert* constant controls whether assertion are checked." | ||
([?schema x] | ||
(if *assert* | ||
`(let [x# ~x] | ||
(if (m/validate ~?schema x#) | ||
x# | ||
(*reporter* ::m/explain (mu/explain-data ~?schema x#)))) | ||
~x))) | ||
|
||
|
||
;; ----------------------------------------------------------------------------------------------------------- | ||
;; Developer Notes | ||
;; | ||
(comment | ||
(assert int? "42") | ||
(assert int? 42) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns malli.assert-test | ||
(:refer-clojure :exclude [assert]) | ||
(:require | ||
[clojure.test :refer [deftest is]] | ||
[malli.assert :refer [assert]])) | ||
|
||
(deftest assert-throws-test | ||
(is (= *assert* true)) | ||
(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 (= *assert* true)) | ||
(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})))) |