Improve min/max validation

This commit is contained in:
Jake Barnby 2025-12-04 00:03:12 +13:00
parent e00dacf9a2
commit 331e294ef5
2 changed files with 6 additions and 5 deletions

View file

@ -234,11 +234,11 @@ class Create extends Action
$formatOptions = ['elements' => $attributeDef['elements']];
}
if (isset($attributeDef['min']) && isset($attributeDef['max'])) {
if (isset($attributeDef['min']) || isset($attributeDef['max'])) {
$format = $type === Database::VAR_INTEGER ? APP_DATABASE_ATTRIBUTE_INT_RANGE : APP_DATABASE_ATTRIBUTE_FLOAT_RANGE;
$formatOptions = [
'min' => $attributeDef['min'],
'max' => $attributeDef['max'],
'min' => $attributeDef['min'] ?? ($type === Database::VAR_INTEGER ? \PHP_INT_MIN : -\PHP_FLOAT_MAX),
'max' => $attributeDef['max'] ?? ($type === Database::VAR_INTEGER ? \PHP_INT_MAX : \PHP_FLOAT_MAX),
];
}

View file

@ -182,13 +182,14 @@ class Attributes extends Validator
}
// Validate min/max range for integer/float
if (isset($attribute['min']) && isset($attribute['max'])) {
if (isset($attribute['min']) || isset($attribute['max'])) {
if (!in_array($attribute['type'], [Database::VAR_INTEGER, Database::VAR_FLOAT])) {
$this->message = "Attribute '" . $attribute['key'] . "': min/max can only be used with integer or float types";
return false;
}
if ($attribute['min'] > $attribute['max']) {
// If both are set, validate ordering
if (isset($attribute['min']) && isset($attribute['max']) && $attribute['min'] > $attribute['max']) {
$this->message = "Attribute '" . $attribute['key'] . "': minimum value must be less than or equal to maximum value";
return false;
}