Clojure SchemaClojure(Script) 库
Schema 是一个 Clojure(Script) 库,用来声明数据描述和验证。
代码示例:
(ns schema-examples (:require [schema.core :as s :include-macros true ;; cljs only ])) (def Data "A schema for a nested data type" {:a {:b s/Str :c s/Int} :d [{:e s/Keyword :f [s/Num]}]}) (s/validate Data {:a {:b "abc" :c 123} :d [{:e :bc :f [12.2 13 100]} {:e :bc :f [-1]}]}) ;; Success! (s/validate Data {:a {:b 123 :c "ABC"}}) ;; Exception -- Value does not match schema: ;; {:a {:b (not (instance? java.lang.String 123)), ;; :c (not (integer? "ABC"))}, ;; :d missing-required-key}
评论