js.spec icon indicating copy to clipboard operation
js.spec copied to clipboard

Compose spec

Open arichiardi opened this issue 8 years ago • 3 comments

One of the great features of clojure.spec is the ability of merging specs, because they are essentially data (ok macros are still in the way, but nothing is perfect).

At the moment js.spec does not do that, but it would be great if we could do something like:

const aSpec = S.spec.map("a spec", {
  a: S.spec.object,
  b: S.spec.object,
});

const composedSpec = S.merge(aSpec , {
  c: S.spec.object
}

Ok the example does not make too much sense, but we should be composing and reusing specs.

...I have had a look at the code and this is actually a big shift because of the use of class...still think is worth doing it...maybe the S.merge function can just merge the fields...

arichiardi avatar Oct 24 '17 23:10 arichiardi

So you're saying neither Spec.or nor Spec.and will work. Can you provide a link to the Clojure Spec docs section that shows how to do this?

mattbishop avatar Nov 03 '17 18:11 mattbishop

It is more on the composition of each individual spec, where one just adds fields on top of the other.

In Clojure spec there is a merge:

(s/def :animal/kind string?)
(s/def :animal/says string?)
(s/def :animal/common (s/keys :req [:animal/kind :animal/says]))
(s/def :dog/tail? boolean?)
(s/def :dog/breed string?)

(s/def :animal/dog (s/merge :animal/common (s/keys :req [:dog/tail? :dog/breed])))

(s/valid? :animal/dog
  {:animal/kind "dog"
   :animal/says "woof"
   :dog/tail? true
   :dog/breed "retriever"})

arichiardi avatar Nov 03 '17 20:11 arichiardi

Does this only apply to map specs?

mattbishop avatar May 04 '18 23:05 mattbishop