Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Last active February 16, 2016 05:07
Show Gist options
  • Save ikitommi/7927145ee263fc2974df to your computer and use it in GitHub Desktop.
Save ikitommi/7927145ee263fc2974df to your computer and use it in GitHub Desktop.
Dummy implementation of Schema Map validator (to get more humane errors)
(require '[schema.core :as s])
(require '[schema.utils :as su])
(defn check-map [schema value]
{:pre [(map? value)]}
(if-let [value (s/check schema value)]
(into {}
(for [[k v] value]
[k (cond
(symbol? v) (keyword v)
(instance? schema.utils.ValidationError v)
(let [[not? [sym val]] (su/validation-error-explain v)]
(if (= not? 'not)
[(keyword sym) val]))
:else :unknown-error)]))))
(check-map {:a (s/maybe (s/constrained s/Int pos? 'should-be-positive)), :b String}
{:a -1})
; => {:a [:should-be-positive -1], :b :missing-required-key}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment