我正在尝试使用 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"}]))
主要问题是我不明白编写允许省略指定向量的第一个元素的模式的方法是什么。从上面匹配两个向量的正确方法是什么?