Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Deraen opened this issue Nov 10, 2021 · 5 comments

Comments

@Deraen
Copy link
Member

Deraen commented Nov 10, 2021

(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
Copy link
Member Author

Deraen commented Nov 10, 2021

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
Copy link
Member Author

Deraen commented Nov 10, 2021

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

@ikitommi
Copy link
Member

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?

@Deraen
Copy link
Member Author

Deraen commented Nov 12, 2021

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

@crimeminister
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants