plumatic/schema

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

Rovanion opened this issue · 3 comments

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 commented

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?

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 commented

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