malli
malli copied to clipboard
Pattern/RegExp class schema doesn't actually check if value is RegExp instance
(m/validate #?(:clj java.util.regex.Pattern, :cljs js/RegExp) #"foo")
;; :malli.core/child-error {:type :re, :properties nil, :children nil, :min 1, :max 1}
I'm quite sure the implementation should check if the value is Regexp instance here.
We don't seem to have any test cases for this.
We could also consider if there should be an easy cross-platform way to create this schema?
https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L2251-L2252
https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L1304
Pattern/RegExp class schema was added here: https://github.com/metosin/malli/pull/68/files
But I don't have idea what it was supposed to do.
Ok, it is for cases like (m/validate #"foo" foo)
. This finds the class-schema implementation because Schema lookup will take type
(class
) of the schema value if a Schema isn't found using the value itself. Quite surprising...
In longer term, might be good just to support :string
with a custom property like: [:string {:pattern ".*"}]
, mimicking json schema. Supporting just string would allow simple identity checks for schemas too. What do you think?
Perhaps.
The use case here was to have a RegExp instance as a value.
(m/validate [Pattern] #"foo")
There doesn't seem to be any way to validate the value is a regexp instance right now. (Without going through :fn
and a predicate.)
I would like to be able to check that a value is a regular expression instance of the platform-specific type (js/RegExp
, java.util.regex.Pattern
) as well. I was surprised to realize that there's no predicate in clojure.core
to support that in a friendly cross-platform way.