Created
December 12, 2012 16:50
-
-
Save aaronc/4269421 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 simpleschema) | |
(defn make-validator | |
([name func] (make-validator name func nil)) | |
([name func params] | |
(with-meta func | |
{:validator-name name | |
:validator-params params}))) | |
(defmacro defvalidator [name validator-args & body] | |
`(def ~name | |
(make-validator ~(keyword name) | |
(fn ~name ~validator-args | |
~@body)))) | |
(defmacro defvalidatorfn [name param-args validator-args & body] | |
(let [args? (first body) | |
fargs? (second body)]) | |
`(defn ~name ~param-args | |
(make-validator ~(keyword name) | |
(fn ~name ~validator-args | |
~@body) | |
~param-args))) | |
(defn- compile-validators [validators] | |
(doseq [validator validators] | |
(println (meta validator)) | |
validators)) | |
(defn schema | |
"" | |
[schema] | |
(let [schema (update-in schema [:properties] | |
(fn [properties] | |
(into {} | |
(for [[k v] properties] | |
[k (update-in v [:validators] compile-validators)]))))]) | |
(update-in schema [:validators] compile-validators)) | |
(defn validate-schema | |
([schema value] | |
()) | |
([schema value opts])) | |
(defn validate-property | |
([schema path value]) | |
([schema path value opts])) | |
(defn get-property-attr [attr schema path]) | |
(defn format-property | |
([schema path value]) | |
([schema path value opts])) | |
(defn parse-property | |
([schema path value]) | |
([schema path value opts])) | |
(defn format-schema | |
([schema value]) | |
([schema value opts])) | |
(defn parse-schema | |
([schema value]) | |
([schema value opts])) | |
;; Test code | |
(defvalidator required | |
[x ctxt] (not (nil? x))) | |
(defvalidatorfn minimum [min-value] | |
[x ctxt] (>= x min-value)) | |
(defvalidatorfn maximum [min-value] | |
[x ctxt] (>= x min-value)) | |
(def test-schema | |
{:properties | |
{:a {:required false | |
:validators [number? (minimum 5) (maximum 10)] | |
:title "A" }}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment