From d08d2f49acbb532c2e664ae7e7fed0fb47a6d40b Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 24 May 2025 11:28:56 +0200 Subject: [PATCH] :sparkles: Add better defaults for several number schema generators --- common/src/app/common/schema.cljc | 20 +++++++++++++------- common/src/app/common/schema/generators.cljc | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index a9bd5443d..b218ea14d 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -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] diff --git a/common/src/app/common/schema/generators.cljc b/common/src/app/common/schema/generators.cljc index 2f90ab226..105f2f276 100644 --- a/common/src/app/common/schema/generators.cljc +++ b/common/src/app/common/schema/generators.cljc @@ -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}}]