Last active
February 16, 2016 05:07
-
-
Save ikitommi/7927145ee263fc2974df to your computer and use it in GitHub Desktop.
Dummy implementation of Schema Map validator (to get more humane errors)
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
(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