clojure - Validating multiple polymorphic values using Prismatic Schema -
clojure - Validating multiple polymorphic values using Prismatic Schema -
a little while ago asked simple polymorphic schemas, , reply there worked time.
now map wish validate has additional value dependent upon key's value.
a contrived illustration of object:
{:type      :foo         {:type      :bert        {:type      :foo  :foo-param :bar          :bert-size :medium       :foo-param :bar  :method    :baz          :method    :baz          :method    :bang  :baz-rate  :max}         :baz-rate  :max}         :bangness  :considerable}    the discriminators here :type , :method, each of has own set of valid sibling keys , values.
previously :type existed, ,  next worked:
(def ^:private test-base-schema {:type (s/enum :foo :abc :banana)})  (def test-schema   (s/conditional #(= (:type %) :foo)                  (merge test-base-schema {:foo-param s/keyword})                  ; other conditions here                  ))    however there more 1 discriminator, number of conditional branches combinatorial.
one  alternative allow {s/any s/any} in maps , using s/both, cannot allow schemas 'loose', unexpected key/values should seen invalid.
i not want alter construction of map beingness validated allow validation work using library.
is there sane way accomplish strict validation of maps have multiple conditional sub-schemas?
a snarky reply may smell info model less ideal, , should consider refactoring have nested construction
{:type-info {:type :foo :foo-param :bar}  :method-info {:method :baz :baz-rate :max}}    a (perhaps) more helpful reply don't think easy out-of-the-box set of schemas, besides writing custom predicate validation.
if isn't desirable, guess need introduce new schema type. fortunately, easy in user code (or third-party library). unfortunately, don't see easy way represent these 2 concepts (strict union , conditional map structure) in crisp , orthogonal way (without combining them single conditional-union schema, or having union conditional-aware). believe there's a way, it's not obvious me @ first look.
 clojure prismatic-schema 
 
Comments
Post a Comment