Fork me on GitHub
#clojure-spec
<
2018-02-04
>
richiardiandrea01:02:21

Is there a way to not report errors for an argument in a s/cat or spec only the second position of it? The first argument in my case is a JS connection object and I am not interested in seeing it when it does not validate because very verbose

seancorfield01:02:01

@richiardiandrea If you don't care about it validating, why not spec it as any??

richiardiandrea01:02:37

Yes that is what I am doing, but it is included as part of the error message

seancorfield01:02:10

Oh, I see. So you want a custom error reporter instead? Have you looked at Expound?

richiardiandrea01:02:55

Oh right, yes I am aware of expound. Probably I can have a custom reporter if not too tough too implement

bbrinck01:02:34

@richiardiandrea With expound, you can customize the function that prints out your value https://github.com/bhb/expound#configuring-the-printer

bbrinck01:02:15

See the example under “You can even provide your own function to display the invalid value.”

bbrinck01:02:27

Although by default, parts of your data that is valid won’t be shown anyway with Expound

bbrinck01:02:24

@richiardiandrea Here’s an example that includes a custom printer that replaces the first arg (presumably one that is verbose) with a more succinct representation https://gist.github.com/bhb/5222914641bcdd08d07b7d930e388d89

richiardiandrea04:02:16

That is great thaaaanks, I am actually using expound

alex-dixon14:02:36

How do you write a spec for a map that has a namespaced keyword like :db/id?

taylor14:02:41

use a s/keys spec, and use its :req or :opt to specify the individual key specs

(s/def my-spec (s/keys :req [:db/id]))

alex-dixon15:02:31

Awesome. Thanks. Thought I had to use ::db/id for some reason

bbrinck22:02:10

Coming soon in expound: optional human-readable error messages for predicates https://asciinema.org/a/161011

stathissideris23:02:17

@bbrinck that’s really good, but would it be possible to add an error message to an existing spec? I’m thinking about the case where you’re trying to do this to a third-party spec whose code you don’t control

stathissideris23:02:44

it may be too much to ask, but I think it would be better to keep the libs off each other’s toes

bbrinck23:02:20

It’s an interesting idea … certainly you could add a an error message for a spec directly (I can add a function for this).

bbrinck23:02:00

If you used expound/def on an existing function, you’d probably need to overwrite the actual spec though, so as not to break the way s/def works

bbrinck23:02:40

in other words, calling expound/def would overwrite existing spec, just like s/def

bbrinck23:02:18

Another thing to watch out for is that this won’t work unless the original spec author defined a spec for the predicate specifically

bbrinck23:02:18

So, for instance, you won’t be able to add an error message for this instance of vector? https://github.com/clojure/core.specs.alpha/blob/master/src/main/clojure/clojure/core/specs/alpha.clj#L16 since it’s not a spec by itself

stathissideris23:02:40

@bbrinck yeah, I would expect the parameter in that function to be the name of a spec, not a predicate

stathissideris23:02:49

I was looking at spec tools recently, and although I liked the functionality, I was a bit sceptical about that fact that I would have to write specs differently (using the “spec record” I think)

stathissideris23:02:06

so that’s what struck me about your approach as well

bbrinck23:02:15

Yep, I could add something like (expound/add-message <qualified-keyword> <string>)

stathissideris23:02:00

I’d be very happy with that 🙂 does that get stored in a separate registry for expound?

bbrinck23:02:07

Yes, that’s correct

bbrinck23:02:46

@stathissideris Yes, needing to write specs differently is a fair concern here. I don’t think there’s a way to bolt this functionality onto existing specs, and even if we could, I’m not sure I’d want to

bbrinck23:02:11

Unless clojure.spec supported this directly, of course … 🙂

stathissideris23:02:34

there’s been talk about spec metadata for quite a while now

bbrinck23:02:37

But for now, you can write your specs in either style

stathissideris23:02:43

…but no official solution yet

bbrinck23:02:05

and if the consumer is using spec, it works. if they are using expound, they will get the enhanced error messages

stathissideris23:02:33

yeah, I think it all composes a bit more nicely like that!

bbrinck23:02:45

(for the record, I’d be happy to see something compatible go into spec, and if it did, i’d use it in expound)

bbrinck23:02:57

oh wait, I actually implemented this function - it’s register-message

bbrinck23:02:12

Man, I’ve already forgotten what I wrote yesterday 🙂

bbrinck23:02:17

I will add it to documentation

stathissideris23:02:21

looking forward to the new functionality, I have a direct use for it when you release it, so thanks for your work!

stathissideris23:02:49

haha, that’s great, that’s the style I’m going to use

bbrinck23:02:59

Cool, I hope to release early this week

bbrinck23:02:02

until then, it’s in 0.4.1-SNAPSHOT

bbrinck23:02:12

but, use at your own risk 🙂

stathissideris23:02:42

not in a hurry, but looking for ways to make the errors in my UI a bit more user-friendly

bbrinck04:02:23

I’ve released Expound 0.5.0, which includes this feature

stathissideris07:02:52

great, thank you very much, I’m going to give it a go in the next few days

stathissideris13:02:09

so, no register-message in the end?