clojure.spec

Meaningful error message for Clojure.Spec validation in :pre

自古美人都是妖i 提交于 2019-12-04 10:07:36
问题 I used the last days to dig deeper into clojure.spec in Clojure and ClojureScript. Until now I find it most useful, to use specs as guards in :pre and :post in public functions that rely on data in a certain format. (defn person-name [person] {:pre [(s/valid? ::person person)] :post [(s/valid? string? %)]} (str (::first-name person) " " (::last-name person))) The issue with that approach is, that I get a java.lang.AssertionError: Assert failed: (s/valid? ::person person) without any

clojure.spec human readable shape?

≯℡__Kan透↙ 提交于 2019-12-04 07:20:48
With clojure.spec, is there a way to define a more “human-readable” spec for nested maps? The following doesn't read very well: (s/def ::my-domain-entity (s/keys :req-un [:a :b]) (s/def :a (s/keys :req-un [:c :d])) (s/def :b boolean?) (s/def :c number?) (s/def :d string?) Given that the shape of a conforming entity is something like {:a {:c 1 :d "hello"} :b false} My complaint is that it becomes hard(er) to read a spec if it has any sort of nested maps or any deep structure… because you are chasing keys up and down a file and they aren’t “in place” declarations. To compare, something like

How can I use my specs for their intended purposes if they are in a separate namespace?

半腔热情 提交于 2019-12-03 10:47:33
问题 One of the examples in the clojure.spec Guide is a simple option-parsing spec: (require '[clojure.spec :as s]) (s/def ::config (s/* (s/cat :prop string? :val (s/alt :s string? :b boolean?)))) (s/conform ::config ["-server" "foo" "-verbose" true "-user" "joe"]) ;;=> [{:prop "-server", :val [:s "foo"]} ;; {:prop "-verbose", :val [:b true]} ;; {:prop "-user", :val [:s "joe"]}] Later, in the validation section, a function is defined that internally conforms its input using this spec: (defn- set

How can I use my specs for their intended purposes if they are in a separate namespace?

Deadly 提交于 2019-12-03 02:18:01
One of the examples in the clojure.spec Guide is a simple option-parsing spec: (require '[clojure.spec :as s]) (s/def ::config (s/* (s/cat :prop string? :val (s/alt :s string? :b boolean?)))) (s/conform ::config ["-server" "foo" "-verbose" true "-user" "joe"]) ;;=> [{:prop "-server", :val [:s "foo"]} ;; {:prop "-verbose", :val [:b true]} ;; {:prop "-user", :val [:s "joe"]}] Later, in the validation section, a function is defined that internally conform s its input using this spec: (defn- set-config [prop val] (println "set" prop val)) (defn configure [input] (let [parsed (s/conform ::config

Where to put specs for Clojure.Spec?

六眼飞鱼酱① 提交于 2019-12-02 16:58:12
So, I'm diving deeper and deeper into Clojure.Spec . One thing I stumbled upon is, where to put my specs . I see three options: Global Spec File In most examples, I found online, there is one big spec.clj file, that gets required in the main namespace. It has all the (s/def) and (s/fdef) for all the "data types" and functions. Pro: One file to rule them all Contra: This file can be big Single Responsibliy Principle violated? Specs in production namespaces You could put your (s/def) and (s/fdef) right next to your production code. So that, the implementation and the spec co-exist in the same

What is Clojure spec?

夙愿已清 提交于 2019-11-30 15:42:30
问题 I could not understand the intent of clojure . spec What kind of problems does it solve? Why should we use it? 回答1: spec allows you to create specifications for data and functions. Specifications are at their core predicative (based on existing Clojure predicates) and structural, rather than type-based as you might see in a statically typed language. By basing spec on predicates, you can write specifications that are far more expressive than most type systems and using the same language as

What is Clojure spec?

旧巷老猫 提交于 2019-11-30 14:34:08
I could not understand the intent of clojure . spec What kind of problems does it solve? Why should we use it? Alex Miller spec allows you to create specifications for data and functions. Specifications are at their core predicative (based on existing Clojure predicates) and structural, rather than type-based as you might see in a statically typed language. By basing spec on predicates, you can write specifications that are far more expressive than most type systems and using the same language as your code. Specs defined on a function specify the specs for the args, the return value, and a