【问题标题】:Prismatic/Schema require key dependency?棱柱形/模式需要关键依赖?
【发布时间】:2021-02-07 22:15:30
【问题描述】:

我正在使用plumatic/schema 和我的compojure-api 来验证端点的输入。

我的架构中有两个键:Field1Field2。我希望能够为我的架构定义一个规则,例如:

WHEN Field1 = "AA" 
THEN Field2 is required-key 
ELSE Field2 is optional-key

但是,我似乎只能将密钥设置为必需或可选。是否可以使一个键依赖于另一个键?

(schema/def Field1
  (schema/enum "AA" "BB"))

(schema/def Field2
  (schema/enum "AAAA" "BBBB" "CCCC"))


(schema/defschema MySchema
  {(schema/required-key :field1)           Field1
   
   ; Here I want some kind of logic to make the key required if 
   (if (= Field1 "AA")
       (schema/required-key :field2)      Field2
       (schema/optional-key :field)      Field2)
   })

【问题讨论】:

    标签: clojure plumatic-schema compojure-api


    【解决方案1】:
    (use '[plumbing.core])
    
    (schema/defschema MySchema
        (schema/conditional
            (fn-> :field1 (= "AA"))
            (schema/schema-with-name {(schema/required-key :field1) Field1
                                      (schema/required-key :field2) Field2} "cond1")
            
            :else
            (schema/schema-with-name {(schema/required-key :field1) Field1
                                            (schema/optional-key :field2) Field2} "cond2")))
    

    【讨论】:

      猜你喜欢
      • 2015-10-13
      • 1970-01-01
      • 2016-03-18
      • 2013-09-30
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多