使用Prismatic模式验证多个多态值 [英] Validating multiple polymorphic values using Prismatic Schema

查看:127
本文介绍了使用Prismatic模式验证多个多态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

稍后我问关于简单多态模式的问题,答案一直很好。

A little while ago I asked about simple polymorphic schemas, and the answer there worked well for a time.

现在,我想验证的地图有一个额外的值,这取决于另一个键的值。

Now the map I wish to validate has an additional value which is dependent upon another key's value.

的对象:

{: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}

这里的鉴别符是:type :方法,每个都有自己的一组有效的同级键和值。

The discriminators here are :type and :method, each of which has its own set of valid sibling keys and values.

:type 存在,并且以下工作:

Previously only :type existed, and the following 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 now that there is more than one discriminator, the number of conditional branches would be combinatorial.

一个选项是允许 {s / Any s / Any} 映射和使用 s / both ,但我不能允许模式是'松散',因为意外的键/值应被视为无效。

One option is to allow {s/Any s/Any} in the maps and using s/both, but I cannot allow the schemas to be 'loose', as unexpected key/values should be seen as invalid.

我也不想更改正在验证的地图的结构,只允许验证使用此库。

I also do not want to change the structure of the map being validated just to allow validation to work using this library.

sane的方式来实现严格验证的地图有多个条件子模式?

Is there a sane way to achieve strict validation of maps that have multiple conditional sub-schemas?

推荐答案

一个snarky的答案是,这可能是一个嗅觉你的数据模型不太理想,你应该考虑重构它具有像

A snarky answer is that this may be a smell that your data model is less than ideal, and you should consider refactoring it to have nested structure like

{:type-info {:type :foo :foo-param :bar}
 :method-info {:method :baz :baz-rate :max}}

(也许)更有帮助的答案是,我不认为这是很容易与开箱即用的模式集,除了只写一个自定义谓词验证。

A (perhaps) more helpful answer is that I don't think this is easy with the out-of-the-box set of schemas, besides just writing a custom predicate for validation.

如果这是不可取的,我想你需要引入一个新的模式类型。幸运的是,这在用户代码(或第三方库)中很容易做到。不幸的是,我没有看到一个简单的方法来以清晰和正交的方式(不将它们组合成一个条件联合模式,或者具有条件感知联合)来表示这两个概念(严格联合和条件映射结构)。我相信有一种方法,但首先看不明显。

If this isn't desirable, I guess you need to introduce a new schema type. Fortunately, this is easy to do in user code (or a third-party library). Unfortunately, I don't see an easy way to represent these two concepts (strict union and conditional map structure) in a crisp and orthogonal way (without combining them into a single conditional-union schema, or having union be conditional-aware). I believe there's a a way, but it's not obvious to me at first look.

这篇关于使用Prismatic模式验证多个多态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆