File tree 3 files changed +32
-0
lines changed
3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ Malli is in well matured [alpha](README.md#alpha).
20
20
* Distribute ` :merge ` over ` :multi ` [ #1086 ] ( https://github.com/metosin/malli/pull/1086 ) , see [ documentation] ( README.md#distributive-schemas )
21
21
* allow ` m/-proxy-schema ` child to be a ` delay `
22
22
* 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 )
23
24
24
25
## 0.16.3 (2024-08-05)
25
26
Original file line number Diff line number Diff line change 1
1
(ns malli.transform
2
2
#? (:cljs (:refer-clojure :exclude [Inst Keyword UUID]))
3
3
(:require [malli.core :as m]
4
+ [clojure.math :as math]
4
5
#?(:cljs [goog.date.UtcDateTime])
5
6
#?(:cljs [goog.date.Date]))
6
7
#? (:clj (:import (java.time Instant ZoneId)
94
95
(defn -number->double [x]
95
96
(if (number? x) (double x) x))
96
97
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
+
97
104
(defn -string->keyword [x]
98
105
(if (string? x) (keyword x) x))
99
106
254
261
'float? -number->float
255
262
'double? -number->double
256
263
'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
+
257
271
#?@(:clj ['uri? -string->uri])
258
272
259
273
:enum {:compile (-infer-child-compiler :decode )}
260
274
:= {:compile (-infer-child-compiler :decode )}
261
275
262
276
:float -number->float
263
277
:double -number->double
278
+ :int -number->long
264
279
:keyword -string->keyword
265
280
:symbol -string->symbol
266
281
:qualified-keyword -string->keyword
Original file line number Diff line number Diff line change 119
119
(is (= 1.0 (mt/-number->double 1 )))
120
120
(is (= " kikka" (mt/-number->double " kikka" ))))
121
121
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
+
122
132
(deftest any->string
123
133
#? (:clj (is (= " 1/2" (mt/-any->string 1/2 ))))
124
134
#? (:clj (is (= " http://example.com" (mt/-any->string (URI. " http://example.com" )))))
140
150
(is (= 1 (m/decode int? " +1" mt/string-transformer)))
141
151
(is (= -1 (m/decode int? " -1" mt/string-transformer)))
142
152
(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)))
143
159
(is (= 1.0 (m/decode double? 1 mt/json-transformer)))
144
160
(is (= 1 (m/decode double? 1 mt/string-transformer)))
145
161
(is (= " 1.0x" (m/decode double? " 1.0x" mt/string-transformer)))
You can’t perform that action at this time.
0 commit comments