malli icon indicating copy to clipboard operation
malli copied to clipboard

Pattern/RegExp class schema doesn't actually check if value is RegExp instance

Open Deraen opened this issue 3 years ago • 5 comments

(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

Deraen avatar Nov 10 '21 17:11 Deraen

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.

Deraen avatar Nov 10 '21 17:11 Deraen

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...

Deraen avatar Nov 10 '21 17:11 Deraen

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?

ikitommi avatar Nov 11 '21 18:11 ikitommi

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.)

Deraen avatar Nov 12 '21 12:11 Deraen

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.

crimeminister avatar Jun 13 '22 19:06 crimeminister