metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.43k stars 204 forks source link

Add :min/:max limits to :map validator #1056

Open irigarae opened 1 month ago

irigarae commented 1 month ago

Hi, I see :min and :max are not supported on :map. It would be a nice addition. It is already present in :map-of.

We had a use-case in which two fields are mandatory and 5 are optional. And at least an extra field out of the 5 optional is mandatory. In our case it would be simple to just add the :min property.

[:map {:min 3}
 [:mandatory1 :string]
 [:mandatory2 :string]
 [:optional1 {:optional true} :double]
 [:optional2 {:optional true} :double]
 [:optional3 {:optional true} :double]
 [:optional4 {:optional true} :double]
 [:optional5 {:optional true} :double]]

We solved it with [:and ,,, [:fn ,,,]] but I think the min/max make sense even if the map properties have been defined. Don't know the reason why it was not there since the beginning, but in case it was part of some decision, I think it would be nice to revisit. Number of entries is completely orthogonal to wether the keys are explicit or not.

Also, similar to :map-of the properties should be propagated to json-schema with :minProperties, :maxProperties.

Thanks