diff --git a/modules/reitit-schema/src/reitit/coercion/schema.cljc b/modules/reitit-schema/src/reitit/coercion/schema.cljc index 022f3872..baf9e309 100644 --- a/modules/reitit-schema/src/reitit/coercion/schema.cljc +++ b/modules/reitit-schema/src/reitit/coercion/schema.cljc @@ -26,11 +26,11 @@ (fn [x] (cond #?@(:clj [(class? x) (.getName ^Class x)]) - (instance? schema.core.OptionalKey x) (pr-str (list 'opt (:k x))) - (instance? schema.core.RequiredKey x) (pr-str (list 'req (:k x))) + (instance? #?(:clj schema.core.OptionalKey :cljs s/OptionalKey) x) (pr-str (list 'opt (:k x))) + (instance? #?(:clj schema.core.RequiredKey :cljs s/RequiredKey) x) (pr-str (list 'req (:k x))) (and (satisfies? s/Schema x) (record? x)) (try (pr-str (s/explain x)) (catch #?(:clj Exception :cljs js/Error) _ x)) - (instance? schema.utils.ValidationError x) (str (su/validation-error-explain x)) - (instance? schema.utils.NamedError x) (str (su/named-error-explain x)) + (instance? #?(:clj schema.utils.ValidationError :cljs su/ValidationError) x) (str (su/validation-error-explain x)) + (instance? #?(:clj schema.utils.NamedError :cljs su/NamedError) x) (str (su/named-error-explain x)) :else x)) schema)) diff --git a/test/cljc/reitit/coercion_test.cljc b/test/cljc/reitit/coercion_test.cljc index 97ab0aae..2e5a9e9d 100644 --- a/test/cljc/reitit/coercion_test.cljc +++ b/test/cljc/reitit/coercion_test.cljc @@ -7,6 +7,7 @@ [reitit.coercion.spec] [reitit.core :as r] [schema.core :as s] + [schema.utils :as su] [spec-tools.data-spec :as ds]) #?(:clj (:import (clojure.lang ExceptionInfo)))) @@ -106,3 +107,19 @@ {:compile coercion/compile-request-coercers})] (is (= {:path {:user-id 123, :company "metosin"}} (:parameters (match-by-path-and-coerce! router "/metosin/users/123")))))) + +(deftest schema-stringify-test + (is (= {"(opt :foo)" #?(:clj "java.lang.String" :cljs "Str") + "(req \"bar\")" #?(:clj "java.lang.String" :cljs "Str")} + (reitit.coercion.schema/stringify + {(s/optional-key :foo) s/Str + (s/required-key "bar") s/Str}))) + + (is (= "(named \"error-object\" \"name\")" + (reitit.coercion.schema/stringify + (new #?(:clj schema.utils.NamedError :cljs su/NamedError) "name" "error-object")))) + + (is (= "(not :foo)" + (reitit.coercion.schema/stringify + (new #?(:clj schema.utils.ValidationError :cljs su/ValidationError) + nil nil (delay :foo) nil)))))