Skip to content

Instantly share code, notes, and snippets.

@bmabey
Created October 2, 2013 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmabey/6801012 to your computer and use it in GitHub Desktop.
Save bmabey/6801012 to your computer and use it in GitHub Desktop.
(ns schema-helpers
(:require [schema.core :as s]))
(defn tuple
"Returns a schema for a sequence containing exactly the schema/name pairs.
To denote an optional element use {schema name} instead of [schema name].
Example:
(def t (tuple [String 'foo] [s/Keyword 'bar] {Number 'num}))
(s/check t [\"blah\" :boo])
==> nil
(s/check t [\"blah\" :boo 23])
==> nil
(s/check t [\"blah\" \"not keyword\"])
==> [nil (named (not (keyword? \"not keyword\")) bar)] "
[& schema-name-pairs]
(->> schema-name-pairs
(mapv (fn [pair]
(if (map? pair)
(apply s/optional (-> pair seq first))
(apply s/one pair))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment