clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
stuartrexking 2020-08-25T05:11:48.056700Z

Is it possible to add a req-un to an existing s/def to create a new s/def. I have a request map with a number of keys, and a response map with all the keys in the request, plus an additional id key. Is there a nice way to do this?

seancorfield 2020-08-25T05:13:45.057500Z

@stuartrexking you can use s/merge to merge two s/keys specs together.

seancorfield 2020-08-25T05:14:26.058Z

(s/merge ::my-spec (s/keys :req-un [::some-new-key]))

stuartrexking 2020-08-25T05:15:11.058400Z

@seancorfield Thank you.

jacklombard 2020-08-25T10:54:50.059Z

Is there a way to get the results of s/valid? and s/expalin-str in one call?

2020-08-25T10:55:49.059300Z

you don't mean like by using juxt or something right?

jacklombard 2020-08-25T10:56:09.059700Z

Nope, want spec itself to return it

jacklombard 2020-08-25T10:56:33.060300Z

so that I can destructure say the validity and the error in the same call

jacklombard 2020-08-25T11:01:02.062700Z

I am using spec for api validation where I check whether the body or the params are valid, if not I return 400 with error message as s/explain-str against the same spec. Of course the error message is not humanized, but we can deal with it. Don’t want spec to run its validation twice since payloads can be huge

alexmiller 2020-08-25T12:10:59.064300Z

You could run s/explain-data, check that, then run the function to produce the string from the data