Fork me on GitHub
#clojure-spec
<
2018-08-11
>
myguidingstar03:08:10

this spec used to work in 0.1.x (s/explain (s/and map? (s/+ (s/or :pair (s/cat :key keyword? :value boolean?)))) {:foo true}) but with 0.2.168 it fails predicate: (or (nil? %) (sequential? %))

myguidingstar03:08:39

I use that spec to conform some hash-map's key/value

myguidingstar03:08:53

but it looks like new clojure.spec use sequential? for s/+ therefore doesn't allow hash-maps

myguidingstar03:08:20

what should I do?

myguidingstar03:08:53

that spec is actually used to describe a flexible dsl composed of vectors and hash-maps (the dsl itself is used as part of om.next queries)

myguidingstar03:08:36

I can't think of any viable workaround

myguidingstar03:08:33

also, is there any reason clojure.spec use sequential? there instead of coll? which is almost identical except that it returns true for sets and maps?

seancorfield04:08:17

@myguidingstar Why not just use (s/map-of keyword? boolean?)

seancorfield04:08:21

(it took me a while to read your spec -- what does s/or even mean with just one named branch?)

lilactown05:08:28

is it possible to have a recursive spec?

myguidingstar08:08:15

@seancorfield yup, that s/or is for named branch

myguidingstar08:08:55

I need to tag the key and value, hence the use of s/cat

myguidingstar09:08:58

so s/map-of doesn't work for me

myguidingstar09:08:09

that spec's purpose is to used with s/conform, kinda like a dsl parser, not just to check validation

seancorfield18:08:47

@myguidingstar I wonder if (s/and map? seq (s/+ ...)) would make it so what you need?

beta103622:08:36

I have specd a domain entity in my application and have to read instances encoded as JSON where the keys are without namespaces. What's the recommended way to validate and convert the JSON encoded instances to the internal representation?

cjsauer13:08:17

@U4844LY6A I wrote a little library that uses spec to automatically qualify/unqualify domain entities. Given that JSON uses string keys, you’ll have to walk/keywordize-keys first on your map as this library currently only works with keyword keys. https://github.com/cjsauer/disqualified/blob/master/README.md

beta103617:08:05

You use qualify-map first and then conform the result to validate the input, right?

cjsauer17:08:01

Correct. qualify-map only translates unqualified keys into qualified keys. It doesn't do any conforming of its own. The source code is less than 50 lines in total if you'd like some inspiration for your own implementation: https://github.com/cjsauer/disqualified/blob/master/src/cjsauer/disqualified.cljc#L32-L47

beta103620:08:50

I've had a look. I'm afraid in order to extend it to my use case (embedded maps, collections, conjunctions, etc.) I'd have to re-implement a lot of spec. handling or this way seems to be particularly tricky.