我正在尝试使用Prismatic / Schema匹配以下序列:
[{:n "some text"}] ; => valid
和
[{:k "some text"} {:n "some text"}] ; => valid
我尝试过:
(s/def Elem3
{:k s/Str})
(s/def Elem2
{:n s/Str})
(s/def Elem
[(s/optional Elem2 "elem2") Elem3])
(s/validate Elem [{:k "huji"}])
;; =>
;; Value does not match schema: [(named {:n missing-required-key, :k
;; disallowed-key} "elem2")]
(s/def Elem
[(s/maybe Elem2) Elem3])
(s/validate Elem [{:k "huji"}])
;; =>
;; [(maybe {:n Str}) {:k java.lang.String}] is not a valid sequence
;; schema; a valid sequence schema consists of zero or more `one`
;; elements, followed by zero or more `optional` elements, followed by
;; an optional schema that will match the remaining elements.
(s/defrecord ElemOption1
[elem3 :- Elem3])
(s/defrecord ElemOption2
[elem2 :- Elem2
elem3 :- Elem3])
(s/def Elem
(s/conditional
#(= 2 (count %)) ElemOption2
:else ElemOption1))
(s/validate Elem [{:k "huji"}])
;; =>
;; Value does not match schema: (not (instance?
;; peg_dsl.standard_app.ElemOption1 [{:k "huji"}]))
主要问题是我不明白写什么方式 允许省略指定向量的第一个元素的模式。 匹配上述两个向量的正确方法是什么?
答案 0 :(得分:1)
你第一次尝试的问题是从一开始
可选意味着它期望{:k s / Str}或者没有任何东西,而且它正在看到
{:n s/Str}
,所以显然不对。
你的第二次尝试有两个问题。 Maybe
可以是价值
或nil
,但它需要存在。你也没有写过
序列模式正确。但是序列模式的问题
是元素需要按顺序s/one* s/optional*
和你
想要s/optional s/one
。
你的第三次尝试更接近,使用条件,但你是 无法匹配,因为您没有验证的实例 记录,你正在验证地图。
解决方案如下所示:
(def ElemKNList [(s/one {:k s/Str} "k") (s/one {:n s/Str} "n")])
(def ElemNList [(s/one {:n s/Str} "n")])
(def Elem (s/conditional #(= 2 (count %)) ElemKNList
:else ElemNList))
(s/validate Elem [{:k "huji"} {:n "huji"}])
=> [{:k "huji"} {:n "huji"}]
(s/validate Elem [{:n "huji"}])
=> [{:n "huji"}]
(s/validate Elem [{:k "huji"}])
=> ExceptionInfo Value does not match schema: [(named {:n missing-required-key, :k disallowed-key} "n")] schema.core/validator/fn--18435 (core.clj:151)