Add better defaults for several number schema generators

This commit is contained in:
Andrey Antukh 2025-05-24 11:28:56 +02:00
parent 8f774a3611
commit d08d2f49ac
2 changed files with 16 additions and 8 deletions

View file

@ -702,7 +702,10 @@
(fn [v]
(and (pred v)
(>= max v)))
pred)]
pred)
gen (or (get props :gen/gen)
(sg/small-int :max max :min min))]
{:pred pred
:type-properties
@ -710,7 +713,7 @@
:description "int"
:error/message "expected to be int/long"
:error/code "errors.invalid-integer"
:gen/gen (sg/small-int :max max :min min)
:gen/gen gen
:decode/string parse-long
:decode/json parse-long
::oapi/type "integer"
@ -768,10 +771,11 @@
(>= max v)))
pred)
gen (sg/one-of
(sg/small-int :max max :min min)
(->> (sg/small-double :max max :min min)
(sg/fmap #(mth/precision % 2))))]
gen (or (get props :gen/gen)
(sg/one-of
(sg/small-int :max max :min min)
(->> (sg/small-double :max max :min min)
(sg/fmap #(mth/precision % 2)))))]
{:pred pred
:type-properties
@ -786,7 +790,9 @@
(register! ::safe-int [::int {:max max-safe-int :min min-safe-int}])
(register! ::safe-double [::double {:max max-safe-int :min min-safe-int}])
(register! ::safe-number [::number {:max max-safe-int :min min-safe-int}])
(register! ::safe-number [::number {:gen/gen (sg/small-double)
:max max-safe-int
:min min-safe-int}])
(defn parse-boolean
[v]

View file

@ -10,6 +10,7 @@
(:require
[app.common.schema.registry :as sr]
[app.common.uri :as u]
[app.common.math :as mth]
[app.common.uuid :as uuid]
[clojure.core :as c]
[clojure.test.check.generators :as tg]
@ -40,7 +41,8 @@
(defn small-double
[& {:keys [min max] :or {min -100 max 100}}]
(tg/double* {:min min, :max max, :infinite? false, :NaN? false}))
(->> (tg/double* {:min min, :max max, :infinite? false, :NaN? false})
(tg/fmap #(mth/precision % 2))))
(defn small-int
[& {:keys [min max] :or {min -100 max 100}}]