Search code examples
clojuredata-generationclojure.specspecification-pattern

Clojure.Spec derive or alias another spec


I'd like to use clojure spec to build up a set of type constraints that can be aliased or further constrained by other specs.

For example, I might have many fields that all need to be valid sanitized markdown.

The following example works for validation (s/valid?) but not for generation (gen/generate)

(s/def ::sanitized-markdown string?)
(s/def ::instruction-list #(s/valid? ::sanitized-markdown %)) ;; works

(gen/generate (s/gen ::instruction-list)) ;; fails

However (gen/generate (s/gen ::sanitized-markdown)) does work.

Is there a way to extend ::instruction-list from ::sanitized-markdown so that it preserves all behavior?


Solution

  • You can alias another spec by providing it directly to s/def:

    (s/def ::instruction-list ::sanitized-markdown)