0

我想使用 clojure 规范来建立一组类型约束,这些约束可以被其他规范别名或进一步约束。

例如,我可能有许多字段都需要有效的清理降价。

以下示例适用于验证 ( s/valid?) 但不适用于生成 ( gen/generate)

(s/def ::sanitized-markdown string?)
(s/def ::instruction-list #(s/valid? ::sanitized-markdown %)) ;; works

(gen/generate (s/gen ::instruction-list)) ;; fails

但是(gen/generate (s/gen ::sanitized-markdown))确实有效。

有没有办法扩展它以保留所有行为::instruction-list::sanitized-markdown

4

2 回答 2

2

您可以通过直接将其提供给另一个规范来别名s/def

(s/def ::instruction-list ::sanitized-markdown)
于 2021-03-28T21:51:16.713 回答
1

您可以s/merge在合并地图规格和s/and其他情况下使用。

(s/def ::sanitized-markdown string?)
(s/def ::instruction-list (s/and ::sanitized-markdown #(> (count %) 10)))

(s/valid? ::instruction-list "abcd")
;; false
(s/valid? ::instruction-list "abcdefghijkl")
;; true

(gen/generate (s/gen ::instruction-list)) 
;; "178wzJW3W3zx2G0GJ1931eEeO"

地图示例

(s/def ::a string?)
(s/def ::b string?)
(s/def ::c string?)
(s/def ::d string?)
(s/def ::first-map (s/keys :opt [::a ::b]))
(s/def ::second-map (s/keys :opt [::c ::d]))
(s/def ::third-map (s/merge ::first-map ::second-map))

(s/valid? ::third-map {:a "1" :d "2"})
;; true
(gen/generate (s/gen ::third-map))
;; {::b "gvQ7DI1kQ9DxG7C4poeWhk553", ::d "9KIp77974TEqs9HCq", ::c "qeSZA8NcYr7UVpJDsA17K"}
于 2021-03-27T08:07:29.117 回答