Fork me on GitHub
#clojure-spec
<
2018-02-19
>
arrdem01:02:29

Is there a spec around for the fn form?

arrdem01:02:22

Hummmm I'm going about this wrong methinks 😛

arrdem01:02:02

Ah clojure.core/fn is a spec ID.

ikitommi06:02:24

the new spec-tools (`0.6.1`) has a version of merge that retains the already conformed values:

(require '[clojure.spec.alpha :as s])
(require '[spec-tools.core :as st])

(s/def ::a (s/and (s/conformer str) string?))
(s/def ::b int?)

(s/def ::am (s/keys :opt-un [::a]))
(s/def ::bm (s/keys :opt-un [::b]))

(s/conform (s/merge ::am ::bm) {:a 1, :b 1})  ; {:a 1, :b 1}
(s/conform (s/merge ::bm ::am) {:a 1, :b 1})  ; {:a "1", :b 1}

(s/conform (st/merge ::am ::bm) {:a 1, :b 1}) ; {:a "1", :b 1}
(s/conform (st/merge ::bm ::am) {:a 1, :b 1}) ; {:a "1", :b 1}

igosuki12:02:06

@ikitommi Do you know if there is any way to conform two key aliases into one ? problematic example :

(s/def ::user
  (s/keys [(s/or ::name1 ::name2)])) 
Spec verifies for me already that it’s one of the other, subsequently, I don’t want to have to write (or (:name1 m) (:name2 m)) in all of my code Is there anyway to coerce these keys into another one ?

mpenet12:02:04

syntax is wrong, I don't think you should use a ns on the or, it should read (or ::foo ::bar)

mpenet12:02:37

@igosuki I dont think you can no, you need to use eval for that, or a macro

igosuki12:02:03

yeah sorry I wrote the ns by reflex, but actually am using just a plain or

igosuki12:02:14

It’s annoying to have a declarative syntax for validation, and then have to write custom code for something that trivial though… something like (alias ::name1 ::name2) would be nice, equivalent :

import com.fasterxml.jackson.annotation.{JsonIgnoreProperties}
case class User(@JsonAlias(Array("name1", "name2") name))

mpenet15:02:12

it would really be handy to have specs for specs, I occasionally write (s/def foo ...) or (s/fdef ::foo ...) and it's far from obvious to trace when this happens

mpenet15:02:23

you get an Illegal arg exception in one case but the error is a bit cryptic:

mpenet15:02:28

:type java.lang.IllegalArgumentException :message Invalid match arg: clojure.spec.test.alpha$spec_checking_fn$fn__2943@786f514d

Ed17:02:14

I'm importing something from some json and would like to use the namespaced map in the rest of the program

misha18:02:23

@igosuki (s/def ::my-alias (s/nonconforming (s/or :n1 ::name1 ::name2)))

misha18:02:01

unless I misunderstood the question

misha21:02:10

@igosuki

(clojure.set/rename-keys {:name1 1 :bar 2} {:name1 :name :name2 :name})
=> {:bar 2, :name 1}

misha21:02:18

you can do it pre- or post- spec, and your further convenience-while-working-with-data-structure is probably irrelevant to spec