Fork me on GitHub
#clojure-spec
<
2016-07-27
>
lvh02:07:23

Hm, I don’t really understand how to use retag

lvh02:07:20

I mean, I can use it with a keyword, but now I have something a bit more complex: a schema is either a reference (`{:$ref “some-string"}`) or a schema with a type

lvh02:07:48

I was hoping to teach the multimethod about that, since s/or doesn’t seem to work

lvh02:07:32

(when I s/or, it complains that it can’t find an appropriate method, which is true I guess; I wasn’t expecting it to even try that branch)

lvh02:07:20

I tried doing with with s/or this way:

(s/def ::schema
  (s/or ::reference
        (fn [m] (contains? m :$ref))
        ::direct-schema
        (fn [m] (contains? m :type))))

lvh02:07:36

Which doesn’t work because spec doesn’t know how to generate schemas from that pred.

lvh02:07:44

I can (s/and map? …) but that doesn’t really make it easier to generate maps

lvh02:07:26

I guess I can just use map? and it can later specify with keys — that appears to sorta work 🙂

xcthulhu15:07:29

(s/def ::$ref uuid?)
(s/def ::type keyword?)
(s/def ::schema (s/or ::reference (s/keys :req-un [::$ref]) ::direct-schema (s/keys :req-un [::type])))

xcthulhu15:07:35

@lvh: I think you are supposed to do it in the style above.

michaeldrogalis15:07:09

Hello. Is it possible to assert properties about keys and values in a map using s/keys without using the registry?

xcthulhu16:07:37

Not that I know of.

Chris O’Donnell16:07:39

@michaeldrogalis: You can put keys into s/keys that aren't in the registry, and the spec will just check for their existence. It is not possible to provide inline specs to validate map values.

michaeldrogalis16:07:36

@codonnell: Thanks for the confirmation.

Alex Miller (Clojure team)16:07:14

new screencast about check and instrument

lvh16:07:50

Awesome, thank you!

jonathanj17:07:29

Are there any thoughts on using spec for something like web form validation? Specifically having some way to generate user-readable messages from failed predicates, i.e. more fine-grained than "Name is wrong"

jonathanj17:07:55

(I don't have much experience with spec, apologies if this is either a really frequent question or a really stupid one.)

dominicm18:07:02

@jonathanj: IMO, the output would be okay for parsing for a web form. However, spec doesn't play too well with cross validation, e.g. "is this email taken in the database?"

dominicm18:07:03

I also found specifying equal fields difficult, with meaningful output, so you'd have a few special cases to handle outside of s/keys

jonathanj18:07:29

Are you referring to the error data when you say output, @dominicm? I don't see how the error data would be suitable for a user-facing form, you need a layer of translation since arbitrary predicate names are unlikely to be of value to users in most circumstances.

dominicm18:07:00

My apologies, suitable for translating to user information.

jonathanj18:07:40

@dominicm: what about involving multiple values in a validation? We have a case where the SSN needs to be validated against the gender of the selected title (don't ask), would this be written as a separate spec that combines the others, with and perhaps?

jonathanj18:07:34

And perhaps more interestingly, would the error data allow me to determine all the participants in the failing validation?

dominicm18:07:42

User forms have plenty of awkward cases, domain logic runs rampant here. I think and would be appropriate yes. You want to ensure the SSN, gender and selected title are all valid before doing the overall validation I assume? That would be s/and. I don't know the api too well though, my interest dwindled when I tried to use spec for web forms and found limitations in spec.

dominicm18:07:33

I did find that the output error data was very thorough though, and you could derive a lot of information from it.

jonathanj18:07:53

@dominicm: You're quite right about awkward cases for user forms, did you go another route in the end?

dominicm18:07:06

I started writing my own validation library.

jonathanj18:07:18

Is the code public?

dominicm18:07:29

Not yet 😞

dominicm18:07:35

I should open source my attempts.

dominicm18:07:41

https://github.com/logaan/vlad I can highly recommend vlad though.

jonathanj18:07:55

I've already written two (JS) validation libraries and they are plagued with reinventing existing predicates. I was hoping to find a better way of layering this on something else.

dominicm18:07:52

vlad has a lot of useful predicates built in. https://github.com/weavejester/valip/blob/master/src/valip/predicates.clj I sometimes take advantage.

jonathanj18:07:50

My experience was that a Boolean is not a good enough result for explaining errors to users, but having to decompose the data after the predicate failed was too much duplication of effort

dominicm18:07:28

Nope, you must attach additional semantics to it. That's where vlad comes in.

dominicm18:07:08

I just realised that the README does not do it justice.

jonathanj18:07:22

Skimming the readme, vlad seems worth further investigation, thanks!

jonathanj18:07:50

@dominicm: Nice, reasonably succinct.

dominicm18:07:58

It's the result of much comparison, I'm glad you can benefit from my research 🙂

bsima19:07:17

does anyone know what this error means?

seancorfield20:07:20

@bsima: Which version of Clojure? That works for me on Alpha 10.

bsima20:07:35

1.9-alpha10

bsima20:07:40

¯\(ツ)

bsima20:07:13

I think a lein clean fixed it

seancorfield20:07:28

lein clean solves so many evils 😈

kendall.buchanan20:07:48

@gfredericks: Just following up real quick on that issue last week where explicit loading of test.check and clojure.spec.test/check lead to MultiFn errors. You mentioned master seemed to fix it. Any thoughts on when the next release will be cut?

gfredericks21:07:28

Probably depends on whether it's still painful for @alexmiller

martinklepsch21:07:25

@dominicm: interesting, didn't know about vlad, looks cool

gfredericks21:07:10

@kendall.buchanan: I'll prioritize getting a good release together ASAP

kendall.buchanan21:07:00

@gfredericks: Thank you. I don’t mean to put pressure on you, or anything, but I do sure appreciate it.

Alex Miller (Clojure team)21:07:44

I’m willing to do a release, just would prefer to do as few as possible as it will take me at least an hour to figure out how again

Alex Miller (Clojure team)21:07:09

@gfredericks: a good change log wrt to things likely to affect spec would also be helpful to have

gfredericks21:07:27

@alexmiller: like to send to you folks or for permanent public consumption?

Alex Miller (Clojure team)21:07:03

public, not looking for anything special for us, just to have a place to start analyzing impact