Skip to content

Commit

Permalink
Fix #569: Correctly handle Pattern/RegExp class
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Nov 10, 2021
1 parent 0dc2e1b commit 60953f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1312,9 +1312,11 @@
(-properties-schema [_ _])
(-children-schema [_ _])
(-into-schema [parent properties [child :as children] options]
(-check-children! :re properties children 1 1)
(when-not class?
(-check-children! :re properties children 1 1))
(let [children (vec children)
re (re-pattern child)
re (when-not class?
(re-pattern child))
form (delay (if class? re (-simple-form parent properties children identity options)))
cache (-create-cache options)]
^{:type ::schema}
Expand All @@ -1323,7 +1325,11 @@
(-to-ast [this _] (-to-value-ast this))
Schema
(-validator [_]
(-safe-pred #(re-find re %)))
(-safe-pred (if class?
(fn [v]
(instance? #?(:clj Pattern, :cljs js/RegExp) v))
(fn [v]
(re-find re v)))))
(-explainer [this path]
(fn explain [x in acc]
(try
Expand Down
5 changes: 5 additions & 0 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2605,3 +2605,8 @@
(is (= ["1"] (m/-vmap str (subvec [1 2] 0 1))))
(is (= ["1"] (m/-vmap str (lazy-seq [1]))))
(is (= ["1" "2"] (m/-vmap str [1 2]))))

(deftest regexp-class-test
(is (nil? (m/schema #?(:clj java.util.regex.Pattern, :cljs js/RegExp))))
(is (true? (m/validate #?(:clj java.util.regex.Pattern, :cljs js/RegExp) #"foo")))
(is (false? (m/validate #?(:clj java.util.regex.Pattern, :cljs js/RegExp) "foo"))))

0 comments on commit 60953f4

Please sign in to comment.