4

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

主な問題は、指定されたベクトルの最初の要素を省略できるスキーマを記述する方法が何であるかを理解していないことです。上記の両方のベクトルを一致させる正しい方法は何ですか?

4

1 に答える 1