8000 Add `:float` support by seanstrom · Pull Request #1055 · metosin/malli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add :float support #1055

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

Merged
merged 15 commits into from
Jun 29, 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
2 changes: 2 additions & 0 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@
(defn -some-schema [] (-simple-schema {:type :some, :pred some?}))
(defn -string-schema [] (-simple-schema {:type :string, :pred string?, :property-pred (-min-max-pred count)}))
(defn -int-schema [] (-simple-schema {:type :int, :pred int?, :property-pred (-min-max-pred nil)}))
(defn -float-schema [] (-simple-schema {:type :float, :pred float?, :property-pred (-min-max-pred nil)}))
(defn -double-schema [] (-simple-schema {:type :double, :pred double?, :property-pred (-min-max-pred nil)}))
(defn -boolean-schema [] (-simple-schema {:type :boolean, :pred boolean?}))
(defn -keyword-schema [] (-simple-schema {:type :keyword, :pred keyword?}))
Expand Down Expand Up @@ -2424,6 +2425,7 @@
:nil (-nil-schema)
:string (-string-schema)
:int (-int-schema)
:float (-float-schema)
:double (-double-schema)
:boolean (-boolean-schema)
:keyword (-keyword-schema)
Expand Down
15 changes: 15 additions & 0 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@
(-> (-min-max schema options)
(update :min #(some-> % double))
(update :max #(some-> % double))))))
(defmethod -schema-generator :float [schema options]
(let [max-float #?(:clj Float/MAX_VALUE :cljs (.-MAX_VALUE js/Number))
min-float (- max-float)
props (m/properties schema options)
min-max-props (-min-max schema options)
infinite? #?(:clj false :cljs (get props :gen/infinite? false))]
(->> (merge {:infinite? infinite?
:NaN? (get props :gen/NaN? false)}
(-> min-max-props
(update :min #(or (some-> % float)
#?(:clj min-float :cljs nil)))
(update :max #(or (some-> % float)
#?(:clj max-float :cljs nil)))))
(gen/double*)
(gen/fmap float))))
(defmethod -schema-generator :boolean [_ _] gen/boolean)
(defmethod -schema-generator :keyword [_ _] gen/keyword)
(defmethod -schema-generator :symbol [_ _] gen/symbol)
Expand Down
5 changes: 5 additions & 0 deletions src/malli/json_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
(defmethod accept 'nat-int? [_ _ _ _] {:type "integer", :minimum 0})
(defmethod accept 'float? [_ _ _ _] {:type "number"})
(defmethod accept 'double? [_ _ _ _] {:type "number"})
(defmethod accept 'float? [_ _ _ _] {:type "number"})
(defmethod accept 'pos? [_ _ _ _] {:type "number" :exclusiveMinimum 0})
(defmethod accept 'neg? [_ _ _ _] {:type "number" :exclusiveMaximum 0})
(defmethod accept 'boolean? [_ _ _ _] {:type "boolean"})
Expand Down Expand Up @@ -163,6 +164,10 @@
(defmethod accept :int [_ schema _ _]
(merge {:type "integer"} (-> schema m/properties (select-keys [:min :max]) (set/rename-keys {:min :minimum, :max :maximum}))))

(defmethod accept :float [_ schema _ _]
(merge {:type "number"}
(-> schema m/properties (select-keys [:min :max]) (set/rename-keys {:min :minimum, :max :maximum}))))

(defmethod accept :double [_ schema _ _]
(merge {:type "number"}
(-> schema m/properties (select-keys [:min :max]) (set/rename-keys {:min :minimum, :max :maximum}))))
Expand Down
2 changes: 1 addition & 1 deletion src/malli/provider.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [malli.core :as m]
[malli.registry :as mr]))

(def -preferences (-> [:int 'integer? :double 'number? :qualified-keyword :keyword :symbol :string :boolean :uuid 'inst?]
(def -preferences (-> [:int 'integer? :double :float 'number? :qualified-keyword :keyword :symbol :string :boolean :uuid 'inst?]
(reverse) (zipmap (drop 1 (range))) (assoc 'any? -14 'some? -13, :or -12, :and -11, :any -10, :some -9)))

(defn -safe? [f & args] (try (apply f args) (catch #?(:clj Exception, :cljs js/Error) _ false)))
Expand Down
27 changes: 26 additions & 1 deletion src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,30 @@
(catch #?(:clj Exception, :cljs js/Error) _ x))
x))

(defn parse-float [s]
#?(:clj
(if (string? s)
(try
(Float/parseFloat s)
(catch NumberFormatException _ nil))
(throw (IllegalArgumentException.
(str "Expected string, got " (if (nil? s) "nil" (-> s class .getName))))))
:cljs
(parse-double s)))

(defn -string->float [x]
(if (string? x)
(or (parse-float x) x)
x))

Comment on lines +70 to +85
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm providing a custom definition for parsing floats from strings. I could not find a function that used Float/parseFloat in this way, so I copied and modified a similar function from Clojure source code.

(defn -string->double [x]
(if (string? x)
(or (parse-double x) x)
x))

(defn -number->float [x]
(if (number? x) (float x) x))

(defn -number->double [x]
(if (number? x) (double x) x))

Expand Down Expand Up @@ -210,6 +229,7 @@
:encode m/-keyword->string}
:symbol {:decode -string->symbol}
:int {:decode -string->long}
:float {:decode -string->float}
:double {:decode -string->double}}
(method -any->string))))

< 6D47 a href="#diff-f1db4a1018d4e69e28af9c60e727edfa2eacfaed84eb2999b5ba6e5f87969f06" id="expand-link-39-diff-f1db4a1018d4e69e28af9c60e727edfa2eacfaed84eb2999b5ba6e5f87969f06" class="js-expand directional-expander single-expander" aria-label="Expand All" data-url="/metosin/malli/blob_excerpt/d8f2ac84a7045ab447a8be513d34eca3f723ffcd?context=pull_request&diff=unified&in_wiki_context&last_left=215&last_right=235&left=231&left_hunk_size=13&mode=100644&path=src%2Fmalli%2Ftransform.cljc&pull_request_id=1863788115&right=251&right_hunk_size=15" data-left-range="216-222" data-right-range="236-242"> Expand All @@ -231,13 +251,15 @@
'qualified-symbol? -string->symbol

'uuid? -string->uuid
'float? -number->float
'double? -number->double
'inst? -string->date
#?@(:clj ['uri? -string->uri])

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

:float -number->float
:double -number->double
:keyword -string->keyword
:symbol -string->symbol
Expand Down Expand Up @@ -285,6 +307,7 @@
'zero? -string->long

:int -string->long
:float -string->float
:double -string->double
:boolean -string->boolean

Expand All @@ -295,7 +318,7 @@
:not= -string->long

'number? -string->double
'float? -string->double
'float? -string->float
'double? -string->double
#?@(:clj ['rational? -string->double])
#?@(:clj ['decimal? -string->decimal])
Expand All @@ -318,6 +341,7 @@
'zero? -any->string

:int -any->string
:float -any->string
:double -any->string
;:boolean -any->string

Expand All @@ -327,6 +351,7 @@
:<= -any->string
:not= -any->string

'float -any->string
'double -any->string}))

;;
Expand Down
18 changes: 18 additions & 0 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,24 @@
[1 3 mt/string-transformer [:int {:encode/string {:enter inc, :leave inc}}]]]
:ast {:type :int, :properties {:min 1, :max 4}}
:form [:int {:min 1, :max 4}]}
:float {:schema [:float {:min 1.0, :max 4.0}]
:validate {:success [1.0 2.2 4.0]
:failure [nil "invalid" 0.5]}
:explain [[1.0]
[false {:schema [:float {:min 1.0, :max 4.0}]
:value false
:errors [{:path []
:in []
:schema [:float {:min 1.0, :max 4.0}]
:value false}]}]]
:decode [["1.1" (float 1.1) mt/string-transformer]
["1.1" "1.1" mt/json-transformer]
[(float 1.2) (float 3.2) mt/string-transformer [:float {:decode/string {:enter inc, :leave inc}}]]]
:encode [[(float 1.1) "1.1" mt/string-transformer]
[(float 1.1) (float 1.1) mt/json-transformer]
[(float 1.2) (float 3.2) mt/string-transformer [:float {:encode/string {:enter inc, :leave inc}}]]]
:ast {:type :float, :properties {:min 1.0, :max 4.0}}
:form [:float {:min 1.0, :max 4.0}]}
:double {:schema [:double {:min 1.0, :max 4.0}]
:validate {:success [1.0 2.2 4.0]
:failure [nil "invalid" 0.5]}
Expand Down
22 changes: 22 additions & 0 deletions test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@
:gen/NaN? true}))
(is (not (test-presence special? nil)))))

(testing "float properties"
(let [infinity? #(or (= % ##Inf)
(= % ##-Inf))
NaN? (fn [x]
(#?(:clj Float/isNaN
:cljs js/isNaN)
x))
is-float? (fn [n]
#?(:clj (instance? Float n)
:cljs (float? n)))
special? #(or (NaN? %)
(infinity? %))
test-presence (fn [f options]
(some f (mg/sample [:float options]
{:size 1000})))]
(is (test-presence #?(:clj (comp not infinity?) :cljs infinity?) {:gen/infinite? true}))
(is (test-presence is-float? {}))
(is (test-presence NaN? {:gen/NaN? true}))
(is (test-presence special? {:gen/infinite? true
:gen/NaN? true}))
(is (not (test-presence special? nil)))))

(testing "qualified-keyword properties"
(testing "no namespace => random"
(is (< 1 (->> (mg/sample [:qualified-keyword {:namespace nil}]
Expand Down
1 change: 1 addition & 0 deletions test/malli/json_schema_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
[:nil {:type "null"}]
[[:string {:min 1, :max 4}] {:type "string", :minLength 1, :maxLength 4}]
[[:int {:min 1, :max 4}] {:type "integer", :minimum 1, :maximum 4}]
[[:float {:min 1, :max 4}] {:type "number", :minimum 1, :maximum 4}]
[[:double {:min 1, :max 4}] {:type "number", :minimum 1, :maximum 4}]
[:keyword {:type "string"}]
[:qualified-keyword {:type "string"}]
Expand Down
6 changes: 6 additions & 0 deletions test/malli/provider_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
[{:name "Tommi", :gender (mp/-hinted "male" :enum)}
{:name (mp/-hinted "Tiina" :string), :gender "female"}]]

[[:map
[:speed #?(:clj :float, :cljs :double)]
[:position [:vector #?(:clj :float, :cljs :double)]]]
[{:speed (float 1.5)
:position [(float 300.33) (float 663.66)]}]]

[[:map
[:id :string]
[:tags [:set :keyword]]
Expand Down
3 changes: 3 additions & 0 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
(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)))
(is (= 1.0 (m/decode float? 1 mt/json-transformer)))
(is (= 1 (m/decode float? 1 mt/string-transformer)))
(is (= "1.0x" (m/decode float? "1.0x" mt/string-transformer)))
(is (= :user/kikka (m/decode keyword? "user/kikka" mt/string-transformer))))
(testing "encode"
(is (= "1" (m/encode int? 1 mt/string-transformer)))
Expand Down
0