/spartan.spec

A spartan version of clojure.spec compatible with babashka

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

spartan.spec

project chat

A spartan implementation of clojure.spec.alpha compatible with babashka (>= 0.2.5) and Clojure.

Rationale

Currently babashka doesn't have a built-in implementation of clojure.spec.alpha. This library can be used meanwhile. If the application of spec in scripting turns out to be useful, babashka will probably bundle a built-in implementation of spec2 at some point which will be more performant than this interpreted version.

Differences with clojure.spec.alpha

  • No generators (yet)
  • No fdef (yet)

Usage:

Usage in a deps.edn project:

{:deps {borkdude/spartan.spec {:git/url "https://github.com/borkdude/spartan.spec"
                               :sha "e5c9f40ebcc64b27b3e3e83ad2a285ccc0997097"}}}

Requiring spartan.spec will create a namespace clojure.spec.alpha for compatibility.

Example

This is an example that you can run with babashka:

(ns expound
  (:require [babashka.deps :as deps]))

(deps/add-deps
 '{:deps {borkdude/spartan.spec {:git/url "https://github.com/borkdude/spartan.spec"
                                 :sha "bf4ace4a857c29cbcbb934f6a4035cfabe173ff1"}
          expound/expound {:mvn/version "0.8.7"}}})

;; Loading spartan.spec will create a namespace clojure.spec.alpha for compatibility:
(require 'spartan.spec)
(require '[clojure.spec.alpha :as s])

;; Expound expects some vars to be there, like `with-gen`. Spartan prints warnings that these are used, but doesn't implement them yet.
(binding [*err* (java.io.StringWriter.)]
  (require '[expound.alpha :as expound]))

(s/def ::a (s/cat :i int? :j string?))

(expound/expound ::a [1 2])

Output:

-- Spec failed --------------------

  [... 2]
       ^

should satisfy

  string?

-- Relevant specs -------

:expound/a:
  (clojure.spec.alpha/cat :i clojure.core/int? :j clojure.core/string?)

-------------------------

Tests

Install babashka and deps.clj. Then run script/test.

License

Copyright © 2020 Michiel Borkent

Distributed under the EPL License. See LICENSE.