Fork me on GitHub
#clojure-spec
<
2020-10-05
>
Jim Newton07:10:09

I'm not a spec user, but I have a few questions about the API. Is it true that spec designates certain symbols as patterns? How can I know whether a given symbol designates a spec pattern? As I understand, given such a symbol, I can call (s/valid? pattern-designator some-value) to determine whether a given value conforms to the pattern. Are such pattern designators always available globally? or can the be defined lexically and thus be invisible to other applications?

Jim Newton08:10:29

Is s/valid? side-effect free assuming the given pattern is side-effect free?

Jim Newton08:10:22

Under which situations might s/valid? throw an exception? Do I need to wrap calls in try/catch ?

alexmiller13:10:36

By “pattern”, I assume you mean “symbolic spec”. Specs have both a spec form (comprised of keyword names, list forms, sets, functions, etc) and a compiled instance form. Spec forms refer to macros that expand to code that emits spec instances. The details of this are quite a bit different in spec 1 and 2. But in both cases the spec forms are available globally as they are defined by macros. s/valid? does not have side effects of its own. s/valid? itself won’t throw but if it includes a predicate, that might throw on a bad input

Jim Newton13:10:40

Is there a predicate I can use to know whether a given form is a valid "symbolic spec" ?

Jim Newton13:10:44

Am I correct that s/valid? will throw an error if you give it an invalid symbolic spec?

alexmiller13:10:00

I think s/spec will either give you a spec instance or an error

alexmiller13:10:06

s/valid? should do the same

Jim Newton13:10:30

that's reasonable, yes. So It could be nice if my application could ask spec whether the given spec instance is valid or not, before putting it in a place where at some time in the future when s/valid? is eventually called, spec will throw an error.

Jim Newton13:10:58

OH, am I correct in assuming that s/valid? checks whether the given data matches the spec? i.e., it does not validate the spec, but rather validates the data...

Jim Newton15:10:48

so which function should I use to detect whether a candidate given to my by the function who called me, is a valid symbolic spec?

alexmiller15:10:59

spec 2 is a bit better in this area (but is not ready for use yet)

vlaaad15:10:50

#(contains? (clojure.spec.alpha/registry) %)

alexmiller15:10:03

that's not what he's asking for afaict

Jim Newton15:10:10

Without knowing spec, it is hard to know really what I'm asking for. But Id like to know whether what I have in variable x is something I can pass as the first argument of s/valid?.

Jim Newton15:10:28

I suppose my first implementation doesn't to need to verify its values, just trust the caller, and let spec sort it out.

Jim Newton16:10:14

looks like everything in that registry is either symbol or a keyword. (distinct( map (fn [x] (or (symbol? x) (keyword? x))) (keys (clojure.spec.alpha/registry)))) returns (true)

borkdude16:10:34

symbols are used for fdefs

alexmiller16:10:00

s/valid? takes spec names, spec forms, and spec objects - there is no method that validates all of those