Skip to content

Commit 130be2a

Browse files
authored
Merge pull request #1100 from metosin/feat/json-integral-decoding
feat: json-transformer decodes 123.0 into 123 for int schemas
2 parents b91fc18 + 513f27d commit 130be2a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Malli is in well matured [alpha](README.md#alpha).
2020
* Distribute `:merge` over `:multi` [#1086](https://github.com/metosin/malli/pull/1086), see [documentation](README.md#distributive-schemas)
2121
* allow `m/-proxy-schema` child to be a `delay`
2222
* Fix `malli.dev.pretty` throws when explaining errors in nested maps [#1094](https://github.com/metosin/malli/issues/1096)
23+
* `json-transformer` decodes 123.0 into 123 for schemas like `:int`, `pos-int?` etc. [#986](https://github.com/metosin/malli/issues/986)
2324

2425
## 0.16.3 (2024-08-05)
2526

src/malli/transform.cljc

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns malli.transform
22
#?(:cljs (:refer-clojure :exclude [Inst Keyword UUID]))
33
(:require [malli.core :as m]
4+
[clojure.math :as math]
45
#?(:cljs [goog.date.UtcDateTime])
56
#?(:cljs [goog.date.Date]))
67
#?(:clj (:import (java.time Instant ZoneId)
@@ -94,6 +95,12 @@
9495
(defn -number->double [x]
9596
(if (number? x) (double x) x))
9697

98+
(defn -number->long [x]
99+
(cond
100+
(integer? x) x
101+
(and (number? x) (== x (math/round x))) (math/round x)
102+
:else x))
103+
97104
(defn -string->keyword [x]
98105
(if (string? x) (keyword x) x))
99106

@@ -254,13 +261,21 @@
254261
'float? -number->float
255262
'double? -number->double
256263
'inst? -string->date
264+
'integer? -number->long
265+
'int? -number->long
266+
'pos-int? -number->long
267+
'neg-int? -number->long
268+
'nat-int? -number->long
269+
'zero? -number->long
270+
257271
#?@(:clj ['uri? -string->uri])
258272

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

262276
:float -number->float
263277
:double -number->double
278+
:int -number->long
264279
:keyword -string->keyword
265280
:symbol -string->symbol
266281
:qualified-keyword -string->keyword

test/malli/transform_test.cljc

+16
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@
119119
(is (= 1.0 (mt/-number->double 1)))
120120
(is (= "kikka" (mt/-number->double "kikka"))))
121121

122+
(deftest number->long
123+
(is (= 1 (mt/-number->long 1.0)))
124+
(is (= 2 (mt/-number->long 2.0)))
125+
(is (= 2.5 (mt/-number->long 2.5)))
126+
(is (= "2.5" (mt/-number->long "2.5")))
127+
#?(:clj (is (= 2 (mt/-number->long 4/2))))
128+
#?(:clj (is (= 2 (mt/-number->long (float 2.0)))))
129+
#?(:clj (is (= 2 (mt/-number->long (double 2.0)))))
130+
(is (= 2 (mt/-number->long 2))))
131+
122132
(deftest any->string
123133
#?(:clj (is (= "1/2" (mt/-any->string 1/2))))
124134
#?(:clj (is (= "http://example.com" (mt/-any->string (URI. "http://example.com")))))
@@ -140,6 +150,12 @@
140150
(is (= 1 (m/decode int? "+1" mt/string-transformer)))
141151
(is (= -1 (m/decode int? "-1" mt/string-transformer)))
142152
(is (= "1" (m/decode int? "1" mt/json-transformer)))
153+
(is (= 1 (m/decode int? 1.0 mt/json-transformer)))
154+
(is (= 1 (m/decode :int 1.0 mt/json-transformer)))
155+
(is (= 1.5 (m/decode int? 1.5 mt/json-transformer)))
156+
(is (= 1.5 (m/decode :int 1.5 mt/json-transformer)))
157+
(is (= 1 (m/decode pos-int? 1.0 mt/json-transformer)))
158+
(is (= 0 (m/decode zero? 0.0 mt/json-transformer)))
143159
(is (= 1.0 (m/decode double? 1 mt/json-transformer)))
144160
(is (= 1 (m/decode double? 1 mt/string-transformer)))
145161
(is (= "1.0x" (m/decode double? "1.0x" mt/string-transformer)))

0 commit comments

Comments
 (0)