Created
October 2, 2013 21:48
-
-
Save bmabey/6801012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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