Clojure.Spec 派生或别名另一个规范
Clojure.Spec derive or alias another spec
我想使用 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))
确实有效。
有没有办法从 ::sanitized-markdown
扩展 ::instruction-list
以保留所有行为?
您可以在合并地图规格时使用 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"}
您可以通过直接向 s/def
提供另一个规范来别名:
(s/def ::instruction-list ::sanitized-markdown)
我想使用 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))
确实有效。
有没有办法从 ::sanitized-markdown
扩展 ::instruction-list
以保留所有行为?
您可以在合并地图规格时使用 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"}
您可以通过直接向 s/def
提供另一个规范来别名:
(s/def ::instruction-list ::sanitized-markdown)