Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: json-transformer decodes 123.0 into 123 for int schemas #1100

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Malli is in well matured [alpha](README.md#alpha).
* Distribute `:merge` over `:multi` [#1086](https://github.com/metosin/malli/pull/1086), see [documentation](README.md#distributive-schemas)
* allow `m/-proxy-schema` child to be a `delay`
* Fix `malli.dev.pretty` throws when explaining errors in nested maps [#1094](https://github.com/metosin/malli/issues/1096)
* `json-transformer` decodes 123.0 into 123 for schemas like `:int`, `pos-int?` etc. [#986](https://github.com/metosin/malli/issues/986)

## 0.16.3 (2024-08-05)

Expand Down
15 changes: 15 additions & 0 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns malli.transform
#?(:cljs (:refer-clojure :exclude [Inst Keyword UUID]))
(:require [malli.core :as m]
[clojure.math :as math]
#?(:cljs [goog.date.UtcDateTime])
#?(:cljs [goog.date.Date]))
#?(:clj (:import (java.time Instant ZoneId)
Expand Down Expand Up @@ -94,6 +95,12 @@
(defn -number->double [x]
(if (number? x) (double x) x))

(defn -number->long [x]
(cond
(integer? x) x
(and (number? x) (== x (math/round x))) (math/round x)
:else x))

(defn -string->keyword [x]
(if (string? x) (keyword x) x))

Expand Down Expand Up @@ -254,13 +261,21 @@
'float? -number->float
'double? -number->double
'inst? -string->date
'integer? -number->long
'int? -number->long
'pos-int? -number->long
'neg-int? -number->long
'nat-int? -number->long
'zero? -number->long

#?@(:clj ['uri? -string->uri])

:enum {:compile (-infer-child-compiler :decode)}
:= {:compile (-infer-child-compiler :decode)}

:float -number->float
:double -number->double
:int -number->long
:keyword -string->keyword
:symbol -string->symbol
:qualified-keyword -string->keyword
Expand Down
16 changes: 16 additions & 0 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@
(is (= 1.0 (mt/-number->double 1)))
(is (= "kikka" (mt/-number->double "kikka"))))

(deftest number->long
(is (= 1 (mt/-number->long 1.0)))
(is (= 2 (mt/-number->long 2.0)))
(is (= 2.5 (mt/-number->long 2.5)))
(is (= "2.5" (mt/-number->long "2.5")))
#?(:clj (is (= 2 (mt/-number->long 4/2))))
#?(:clj (is (= 2 (mt/-number->long (float 2.0)))))
#?(:clj (is (= 2 (mt/-number->long (double 2.0)))))
(is (= 2 (mt/-number->long 2))))

(deftest any->string
#?(:clj (is (= "1/2" (mt/-any->string 1/2))))
#?(:clj (is (= "http://example.com" (mt/-any->string (URI. "http://example.com")))))
Expand All @@ -140,6 +150,12 @@
(is (= 1 (m/decode int? "+1" mt/string-transformer)))
(is (= -1 (m/decode int? "-1" mt/string-transformer)))
(is (= "1" (m/decode int? "1" mt/json-transformer)))
(is (= 1 (m/decode int? 1.0 mt/json-transformer)))
(is (= 1 (m/decode :int 1.0 mt/json-transformer)))
(is (= 1.5 (m/decode int? 1.5 mt/json-transformer)))
(is (= 1.5 (m/decode :int 1.5 mt/json-transformer)))
(is (= 1 (m/decode pos-int? 1.0 mt/json-transformer)))
(is (= 0 (m/decode zero? 0.0 mt/json-transformer)))
(is (= 1.0 (m/decode double? 1 mt/json-transformer)))
(is (= 1 (m/decode double? 1 mt/string-transformer)))
(is (= "1.0x" (m/decode double? "1.0x" mt/string-transformer)))
Expand Down