Fork me on GitHub
#clojure-spec
<
2016-06-12
>
patrkris08:06:31

Hi Everyone. How do you name aggregate stuff with namespaced keywords? Say I have a map that would look like this without namespaced keywords

{:first-name "John"
 :last-name "Doe"
 :address
  {:street "Example Street"
   :street-number "413"
   :city "Example city"}}
The keys at the first level might be namespaced like :customer/first-name and :customer/last-name, but how would I name the keys inside the address map?

hiredman08:06:24

{:customer/first-name "John"
 :customer/last-name "Doe"
 :customer/street "Example Street"
 :customer/street-number "413"
 :customer/city "Example city"
 :customer/address-fields #{:customer/street
                            :customer/street-number
                            :customer/city}}

patrkris08:06:08

@hiredman: thanks. when would you say it's appropriate to create a "child namespace", e.g. :customer.address/*?

sander14:06:40

What is more idiomatic,

(ns work.invoice
  (:require [clojure.spec :as s]))
(s/def ::invoice (s/keys :req [::number ::date ::amount]))
or
(ns work.core
  (:require [clojure.spec :as s]
            [work.invoice :as i))
(s/def ::invoice (s/keys :req [::i/number ::i/date ::i/amount]))
? The first is less typing work, but I don't really like :work.invoice/invoice as a spec name. Maybe this?
(ns work.invoice
  (:require [clojure.spec :as s]))
(s/def :work/invoice (s/keys :req [::number ::date ::amount]))

wotbrew17:06:35

Does anyone know of a way to compose key sets? e.g something like (s/merge ::foo ::bar) edit: and works for validation, but does not yield a working generator...

t3chnoboy17:06:17

Hi! I’m trying to validate a javascript class and not sure how to make it work:

(s/def ::transport (s/or :websocket js/WebSocket
                         :long-poll js/Phoenix.LongPoll))

(s/conform ::transport js/WebSocket)
For objects I use #(instance? Class %) and it works as expected.

t3chnoboy17:06:02

ok, I’ve just made it work:

(s/def ::transport (s/or :websocket #(= js/WebSocket %)
                         :long-poll #(= js/Phoenix.LongPoll %)))

(s/conform ::transport js/WebSocket)