Clojure prismatic/schema defrecord是否没有枚举运行时验证?
使用prismatic/schema时,defrecord上的枚举验证不起作用,如下所示:Clojure prismatic/schema defrecord是否没有枚举运行时验证?,clojure,clojurescript,plumatic-schema,Clojure,Clojurescript,Plumatic Schema,使用prismatic/schema时,defrecord上的枚举验证不起作用,如下所示: (s/defrecord Action [type :- (s/enum :a :b)]) #'user/strict-map->Action user> (Action. "3") ; this should fail #user.Action{:type "3"} user> (Action. 1) ; this should fail
(s/defrecord Action [type :- (s/enum :a :b)])
#'user/strict-map->Action
user> (Action. "3") ; this should fail
#user.Action{:type "3"}
user> (Action. 1) ; this should fail
#user.Action{:type 1}
user> (Action. "abc") ; this should fail
#user.Action{:type "abc"}
但是,当我将enum更改为long时,它会按预期工作:
(s/defrecord ThisWorks [type :- long])
#'user/strict-map->ThisWorks
user> (ThisWorks. 3)
#user.ThisWorks{:type 3}
user> (ThisWorks. "abc")
ClassCastException java.lang.String cannot be cast to java.lang.Number user/eval11888 (form-init4803894880546699153.clj:1)
有人知道吗?非常感谢。因为您可以知道,在将记录传递到函数中之前,您的记录实际上不会被检查:
(s/defrecord Action [type :- (s/enum :a :b)])
(s/defn process-action [x :- Action])
(process-action (Action. "3")) ;; => Exception
关于long
的神奇工作。这只是
字段可以有类型提示,也可以是基元
- 请注意,当前 非基元类型的类型提示将不用于约束 字段类型或构造函数参数,但将用于优化其 在类方法中使用
- 约束字段类型和构造函数 arg已计划好
在哪里?
来自哪里
(s/defrecord PrimitveRec [foo :- long])
(s/defrecord NonPrimitveRec [foo :- String])
(.? NonPrimitveRec :field #"foo" :type)
;=> (java.lang.Object)
(.? PrimitveRec :field #"foo" :type)
;=> (long)