Skip to content

Commit

Permalink
Return original value when trying to coerce! on simple keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Oct 19, 2018
1 parent 3557fcc commit ab2867c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [1.0.0-alpha8]
- Return original value when trying to `coerce!` on simple keywords

## [1.0.0-alpha7]
- `inst?` coercion now accepts a wider range of date & date/time patterns
- `spec-coerce.core/*inst-format*` is dynamic and can be rebound if you need more formats
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Examples from predicate to coerced value:

;; Throw exception when coercion fails
(sc/coerce! `int? "abc") ; => throws (ex-info "Failed to coerce value" {:spec `int? :value "abc"})
(sc/coerce! :simple-keyword "abc") ; => "abc", coerce! doesn't do anything on simple keywords

;; Conform the result after coerce
(sc/conform `(s/or :int int? :bool boolean?) "40") ; [:int 40]
Expand Down
12 changes: 7 additions & 5 deletions src/spec_coerce/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@
"Like coerce, but will call s/assert on the result, making it throw an error if value
doesn't comply after coercion."
[k x]
(let [coerced (coerce k x)]
(if (s/valid? k coerced)
coerced
(throw (ex-info "Failed to coerce value" {:spec k
:value x})))))
(if (simple-keyword? k)
x
(let [coerced (coerce k x)]
(if (s/valid? k coerced)
coerced
(throw (ex-info "Failed to coerce value" {:spec k
:value x}))))))

(defn conform
"Like coerce, and will call s/conform on the result."
Expand Down
1 change: 1 addition & 0 deletions test/spec_coerce/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

(deftest test-coerce!
(is (= (sc/coerce! ::infer-int "123") 123))
(is (= (sc/coerce! :infer-int "123") "123"))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs js/Error) #"Failed to coerce value" (sc/coerce! ::infer-int "abc"))))

(deftest test-conform
Expand Down

0 comments on commit ab2867c

Please sign in to comment.