If statement clojure模式的验证

If statement clojure模式的验证,if-statement,clojure,plumatic-schema,If Statement,Clojure,Plumatic Schema,我在验证clojure棱柱模式时遇到问题。这是代码 :Some_Var1 {:Some_Var2 s/Str :Some_Var3 ( s/conditional #(= "mytype1" (:type %)) s/Str #(= "mytype2" (:type %)) s/Str )} 我正在尝试使用以下代码对其进行验证

我在验证clojure棱柱模式时遇到问题。这是代码

:Some_Var1 {:Some_Var2  s/Str
                  :Some_Var3 ( s/conditional
                        #(= "mytype1" (:type %)) s/Str
                        #(= "mytype2" (:type %)) s/Str
                  )}
我正在尝试使用以下代码对其进行验证:

"Some_Var1": {
    "Some_Var2": "string",
"Some_Var3": {"mytype1":{"type":"string"}}
  }
但这给了我一个错误:

{
  "errors": {
    "Some_Var1": {
      "Some_Var3": "(not (some-matching-condition? a-clojure.lang.PersistentArrayMap))"
    }
  }
}
这是我试图验证的一个非常基本的代码。我对clojure很陌生,仍在努力学习它的基础知识


谢谢,

欢迎来到Clojure!这是一门很棒的语言

在Clojure中,关键字和字符串是不同的类型,即
:type
不是相同的
“type”
。例如:

user=> (:type  {"type" "string"})
nil
(:type  {:type "string"})
"string"
但是,我认为这里有一个更深层次的问题:从查看数据来看,似乎您希望在数据本身中对类型信息进行编码,然后根据该信息进行检查。这可能是可能的,但这将是模式的一种非常高级的用法。模式通常用于类型在类型之前已知的情况,例如数据如下:

(require '[schema.core :as s])
(def data
  {:first-name "Bob"
   :address {:state "WA"
             :city "Seattle"}})

(def my-schema
  {:first-name s/Str
   :address {:state s/Str
             :city s/Str}})

(s/validate my-schema data)
我建议,如果您需要基于编码类型信息进行验证,那么为其编写自定义函数可能会更容易

希望有帮助

更新:

下面是一个关于
条件
如何工作的示例,这是一个将验证的模式,但同样,这是模式的非惯用用法:

(s/validate
{:some-var3
(s/conditional
 ;; % is the value: {"mytype1" {"type" "string"}}
 ;; so if we want to check the "type", we need to first
 ;; access the "mytype1" key, then the "type" key
 #(= "string" (get-in % ["mytype1" "type"]))
 ;; if the above returns true, then the following schema will be used.
 ;; Here, I've just verified that
 ;; {"mytype1" {"type" "string"}}
 ;; is a map with key strings to any value, which isn't super useful
 {s/Str s/Any}
)}
{:some-var3 {"mytype1" {"type" "string"}}})

我希望这能有所帮助。

谢谢你的回复,但我仍然无法解决这个问题。请告诉我条件类型需要什么类型的结构化输入。如何在条件语句中选择mytype1。使用
conditional
,将按顺序计算每个子句。在您的示例中,%的值为:{“mytype1”{“type”“string”}。请注意,此值没有键
:type
,即使它有键,
:type
映射到
“string”
,而不是
“mytype”