Clojure Spec访问分层规范中的数据

时间:2017-10-11 10:22:44

标签: clojure clojure.spec

如果您有一组用于验证分层数据集的规范 - 例如yaml文件。从其中一个子规范中,是否可以引用树中较早出现的数据?

1 个答案:

答案 0 :(得分:0)

这是您可以采取的一种方法的示例:

(s/def ::tag string?)

(s/def ::inner (s/keys :req-un [::tag]))

(s/def ::outer
  (s/and
    (s/keys :req-un [::inner ::tag])
    #(= (:tag %) ;; this tag must equal inner tag
        (:tag (:inner %)))))

(s/conform ::outer {:tag "y" ;; inner doesn't match outer
                    :inner {:tag "x"}})
;=> :clojure.spec.alpha/invalid

(s/conform ::outer {:tag "x"
                    :inner {:tag "x"}})
;=> {:tag "x", :inner {:tag "x"}}

根据您的要求,您可以从外到内而不是从内到外做出这样的断言。

相关问题