Skip to content

Commit

Permalink
added after-transform
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Mar 31, 2013
1 parent c499057 commit b3b4b33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/clj_schema/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,23 @@ map under-validation to have more keys than are specified in the schema."
(def ^{:doc "Wraps a schema to make it a schema that apply to every element of a set"}
set-of set-schema)

(defn after-transform
"Wraps a schema. Returns a new schema that is a String that when read matches
the wrapped schema.
Ex. (validation-errors (after-transform deref (sequence-of Long)) (atom [1 2 3]))
;; => #{}"
[transform-var schema]
(assoc (ensure-schema schema) :pre-validation-transform transform-var))

(defn ->string-schema
"Wraps a schema. Returns a new schema that is a String that when read matches
the wrapped schema.
Ex. (validation-errors (->string-schema (sequence-of Long)) \"[55, -33]\")
;; => #{}"
[schema]
(assoc (ensure-schema schema) :pre-validation-transform #'read-string))
(after-transform #'read-string (ensure-schema schema)))


;; Wildcard Paths
Expand Down
3 changes: 3 additions & 0 deletions test/clj_schema/validation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@
(is (= #{"Value 55 could not be transformed before validation using '#'clojure.core/read-string'."}
(validation-errors (->string-schema [Long pos?]) 55))))

(deftest test-after-transform
(is (= #{} (validation-errors (after-transform #'deref (sequence-of Long)) (atom [1 2 3])))))

(deftest test-seq-layouts
(is (= #{}
(validation-errors checkers-board-schema [[1 0 1 0 1 0 1 0]
Expand Down

0 comments on commit b3b4b33

Please sign in to comment.