From aa4184c040328db8303c7423366f15400155b888 Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Wed, 28 Aug 2024 11:44:20 +0300 Subject: [PATCH 1/2] feat: json-transformer decodes 123.0 into 123 for int schemas fixes #986 --- src/malli/transform.cljc | 15 +++++++++++++++ test/malli/transform_test.cljc | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index d8f2ac84a..5cc6e7877 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -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) @@ -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)) @@ -254,6 +261,13 @@ '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)} @@ -261,6 +275,7 @@ :float -number->float :double -number->double + :int -number->long :keyword -string->keyword :symbol -string->symbol :qualified-keyword -string->keyword diff --git a/test/malli/transform_test.cljc b/test/malli/transform_test.cljc index 6c9cbfe6e..931f57432 100644 --- a/test/malli/transform_test.cljc +++ b/test/malli/transform_test.cljc @@ -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"))))) @@ -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))) From 513f27d76e85468960003c6d024afe57707587c6 Mon Sep 17 00:00:00 2001 From: Joel Kaasinen Date: Wed, 28 Aug 2024 11:47:20 +0300 Subject: [PATCH 2/2] doc: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f4ba57d..ff3ab79bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)