Add :decimal schema#1285
Conversation
Introduce support for a :decimal primitive type across the library. Adds -decimal-schema (predicate uses decimal? on clj, false on cljs) and registers :decimal in the builtin type map. Implements a CLJ-only generator that maps double values to BigDecimal via gen-fmap + bigdec. Adds CLJ-only transformer entries for string<->decimal and any->string conversion. Updates tests: expands core tests to include validation/encode/decode/ast expectations for :decimal and adds a CLJ-only generator test to ensure produced values are decimals within bounds. Uses reader conditionals to keep CLJS-safe behavior.
| (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 -decimal-schema [] (-simple-schema {:type :decimal, :pred #?(:clj decimal? :cljs (constantly false)), :property-pred (-min-max-pred nil)})) |
There was a problem hiding this comment.
If the schema is meaningless in cljs then I propose :decimal not exist on that platform.
| (defmethod -schema-generator :int [schema options] (gen/large-integer* (-min-max schema options))) | ||
| (defmethod -schema-generator :double [schema options] (double-gen schema options)) | ||
| (defmethod -schema-generator :float [schema options] (double-gen schema options)) | ||
| #?(:clj (defmethod -schema-generator :decimal [schema options] (gen-fmap bigdec (double-gen schema options)))) |
There was a problem hiding this comment.
I don't think the current implementation works with :gen/infinite true and :gen/NaN? true.
It might make sense to never generate those values as in spec's generator:
| #?(:clj | ||
| (deftest decimal-generator-test | ||
| (is (every? decimal? (mg/sample [:decimal {:min 0.0M, :max 1.0M}] {:size 100}))) | ||
| (is (every? #(<= 0.0M % 1.0M) (mg/sample [:decimal {:min 0.0M, :max 1.0M}] {:size 100}))))) |
There was a problem hiding this comment.
Please add some tests for :gen/min and :gen/max (see existing for other schemas), and some tests providing :min/:max as longs/doubles as in malli.generator-test/double-with-long-min-test.
Also tests that provide bounds outside the range of Double, for example {:min (inc (bigdec Double/MAX_VALUE))} should generate numbers greater than (bigdec Double/MAX_VALUE).
There was a problem hiding this comment.
Actually I'm not sure if anyone has released a BigDecimal generator that supports arbitrary bounds.
This is the closest I could find:
I think at minimum we need a test to show that it throws rather than violating the expected bounds.
Introduce support for a
:decimalprimitive type across the library.-decimal-schema(predicate:decimal?on CLJ,falseon CLJS) and registers:decimalin the builtin type map.BigDecimalviagen-fmap+bigdec.:decimal.Closes #1272