当我有一个定义如下的架构的记录时:
(schema.core/defrecord Account [id :- schema/Uuid
short-id :- schema/Str
name :- schema/Str
created-at :- schema/Inst])
如何提取架构以应用于包含这些值的哈希图?
原因是HTTP服务接收地图并自动应用模式,如果我只使用帐户,则由于地图不是帐户类型而失败。
。我尝试从解释中提取,如:
(schema.core/explain Account)
但是我得到的不是真正的模式:
{:id Uuid,
:short-id Str,
:name Str,
:created-at Inst}
值是符号而不是类,因此,如果您尝试使用它:
(schema.core/validate (last (schema.core/explain server.models.account.Account)) {})
您会遇到此错误:
IllegalArgumentException No implementation of method: :spec of protocol: #'schema.core/Schema found for class: clojure.lang.Symbol clojure.core/-cache-protocol-fn (core_deftype.clj:568)
您可以使用内置的explain
函数:
(last (schema.core/explain Account))
{:id Uuid, :short-id Str, :name Str, :created-at Inst}
或更棘手的spec
(-> (schema.core/spec Account)
:options
first
:schema
last
last)
{:id java.util.UUID, :short-id java.lang.String, :name java.lang.String, :created-at java.util.Date}