Use namespace alias to refer Record types in schema coercion ns

Since Cljs 1.10.439 using qualified name hasn't worked to refer to
record types.
This commit is contained in:
Juho Teperi 2022-03-02 12:54:03 +02:00
parent acfc48faa1
commit e7a480366f
2 changed files with 21 additions and 4 deletions

View file

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

View file

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