(ns clj-record.validation
  (:use clj-record.util)
  (:use clj-record.core))


(defn- validations-for [model-name] ((@all-models-metadata model-name) :validations))

(defn validate [model-name record]
  (reduce
    (fn [errors [attr message validation-fn]]
      (if (validation-fn (record attr))
        errors
        (merge-with
          (fn [result addl-val] (apply conj result addl-val))
          errors
          {attr [message]})))
    {}
    (validations-for model-name)))

(defn- validates [model-name attribute-name message function]
  (dosync
    (let [metadata (@all-models-metadata model-name)
          validations (or (@metadata :validations) [])]
      (ref-set metadata
        (assoc @metadata :validations (conj validations [(keyword (name attribute-name)) message (eval function)])))))
  '(identity 1)) ; XXX: Have to return a no-op form since we don't need any defs.

(ns clj-record.validation
  (:use clj-record.util)
  (:use clj-record.core))


(defn- validations-for [model-name] ((@all-models-metadata model-name) :validations))

(defn validate [model-name record]
  (reduce
    (fn [errors [attr message validation-fn]]
      (if (validation-fn (record attr))
        errors
        (merge-with
          (fn [result addl-val] (apply conj result addl-val))
          errors
          {attr [message]})))
    {}
    (validations-for model-name)))

(defn- validates [model-name attribute-name message function]
  (dosync
    (let [metadata (@all-models-metadata model-name)
          validations (or (@metadata :validations) [])]
      (ref-set metadata
        (assoc @metadata :validations (conj validations [(keyword (name attribute-name)) message (eval function)])))))
  nil)

Remove the no-op form returned by validates now that we check for a return value before including anything in the generated init-model expansion.
Remove the no-op form returned by validates now that we check for a return value before including anything in the generated init-model expansion.

duelinmarkers/clj-record