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

Defschema doesn't accept java.util.regex.Pattern even though it's a valid schema #391

Closed
Rovanion opened this issue Apr 28, 2017 · 3 comments · Fixed by #443
Closed

Defschema doesn't accept java.util.regex.Pattern even though it's a valid schema #391

Rovanion opened this issue Apr 28, 2017 · 3 comments · Fixed by #443

Comments

@Rovanion
Copy link

Rovanion commented Apr 28, 2017

A regular expression is a valid schema:

(def two-char #"^..$")
(schema/check two-char "hi") ; => nil
(schema/check two-char "h")  ; => (not (re-find #"^..$" "h"))

But it is not accepted by defschema:

(defschema two-char #"^..$")
; Throws:
; 1. Caused by java.lang.ClassCastException
;    java.util.regex.Pattern cannot be cast to clojure.lang.IObj
@w01fe
Copy link
Member

w01fe commented Apr 28, 2017

s/defschema uses metadata so you can't use it on schemas that aren't IMeta. Two workarounds:

  • just use def (def two-char #"^..$")
  • use defschema with a named wrapper (defschema two-char (s/named #"^..$" 'two-char)).

Either of those work for you?

@Rovanion
Copy link
Author

Yeah, I'm currently using the first method.

Is it at all possible to modify defschema so that it wraps the regex in something implementing IMeta? Or will that wreak all sorts of havoc in other places?

@w01fe
Copy link
Member

w01fe commented Apr 28, 2017

Eh, I think wrapping would potentially cause more problems than it solves. We would definitely take a PR that improves the error message though.

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

Successfully merging a pull request may close this issue.

2 participants