1

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

4

1 回答 1

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"}}

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

于 2017-10-11T14:20:24.653 回答