指定记录和地图的详细方式更少? [英] less verbose ways of spec'ing records and maps?
问题描述
与官方规范指南中介绍的方式相比,为地图或记录制定规范的方式是否更为冗长?
Is there a less verbose way of making a spec for a map or a record than the way it is presented in the official spec guide?
(defrecord Person [first-name last-name email phone])
(s/def ::first-name string?)
(s/def ::last-name string?)
(s/def ::email ::email-type)
(s/def ::person (s/keys :req-un [::first-name ::last-name ::email]
:opt-un [::phone]))
理想情况是很好,如果我能写这样的话
ideally it would be nice if I could just write something like
(defrecord Person [first-name last-name email phone])
(s/def ::person (s/keys :req-un [:first-name string?
:last-name string?
:email ::email-type]
:opt-un [:phone]))
推荐答案
这是Clojure团队的有意识决定以分别指定值和键集(您可以阅读此讨论以进行详细说明)。您建议的内容被视为clojure.spec中的反模式,并且被故意 。
It is Clojure team's conscious decision to have values and key sets spec'ed separately (you can read this discussion for a longer explanation). What you suggest is considered an anti-pattern in clojure.spec and was deliberately made not possible.
如果您要使用单语句嵌套结构规范,最好使用 Plumatic Schema 。
If you want single-statement nested structure specs, it is probably better to use Plumatic Schema.
这篇关于指定记录和地图的详细方式更少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!