mirror of
https://github.com/symfony/ai.git
synced 2026-03-23 23:42:18 +01:00
[Platform] Fix number constraints in #[With]
This commit is contained in:
@@ -35,12 +35,12 @@ final class With
|
||||
public readonly ?int $minLength = null,
|
||||
public readonly ?int $maxLength = null,
|
||||
|
||||
// integer
|
||||
public readonly ?int $minimum = null,
|
||||
public readonly ?int $maximum = null,
|
||||
public readonly ?int $multipleOf = null,
|
||||
public readonly ?int $exclusiveMinimum = null,
|
||||
public readonly ?int $exclusiveMaximum = null,
|
||||
// number
|
||||
public readonly int|float|null $minimum = null,
|
||||
public readonly int|float|null $maximum = null,
|
||||
public readonly int|float|null $multipleOf = null,
|
||||
public readonly int|float|null $exclusiveMinimum = null,
|
||||
public readonly int|float|null $exclusiveMaximum = null,
|
||||
|
||||
// array
|
||||
public readonly ?int $minItems = null,
|
||||
@@ -97,46 +97,16 @@ final class With
|
||||
}
|
||||
}
|
||||
|
||||
if (\is_int($minimum)) {
|
||||
if ($minimum < 0) {
|
||||
throw new InvalidArgumentException('Minimum must be greater than or equal to 0.');
|
||||
}
|
||||
|
||||
if (\is_int($maximum)) {
|
||||
if ($maximum < $minimum) {
|
||||
throw new InvalidArgumentException('Maximum must be greater than or equal to minimum.');
|
||||
}
|
||||
}
|
||||
if (null !== $minimum && null !== $maximum && $maximum < $minimum) {
|
||||
throw new InvalidArgumentException('Maximum must be greater than or equal to minimum.');
|
||||
}
|
||||
|
||||
if (\is_int($maximum)) {
|
||||
if ($maximum < 0) {
|
||||
throw new InvalidArgumentException('Maximum must be greater than or equal to 0.');
|
||||
}
|
||||
if (null !== $multipleOf && $multipleOf < 0) {
|
||||
throw new InvalidArgumentException('MultipleOf must be greater than or equal to 0.');
|
||||
}
|
||||
|
||||
if (\is_int($multipleOf)) {
|
||||
if ($multipleOf < 0) {
|
||||
throw new InvalidArgumentException('MultipleOf must be greater than or equal to 0.');
|
||||
}
|
||||
}
|
||||
|
||||
if (\is_int($exclusiveMinimum)) {
|
||||
if ($exclusiveMinimum < 0) {
|
||||
throw new InvalidArgumentException('ExclusiveMinimum must be greater than or equal to 0.');
|
||||
}
|
||||
|
||||
if (\is_int($exclusiveMaximum)) {
|
||||
if ($exclusiveMaximum < $exclusiveMinimum) {
|
||||
throw new InvalidArgumentException('ExclusiveMaximum must be greater than or equal to exclusiveMinimum.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (\is_int($exclusiveMaximum)) {
|
||||
if ($exclusiveMaximum < 0) {
|
||||
throw new InvalidArgumentException('ExclusiveMaximum must be greater than or equal to 0.');
|
||||
}
|
||||
if (null !== $exclusiveMinimum && null !== $exclusiveMaximum && $exclusiveMaximum < $exclusiveMinimum) {
|
||||
throw new InvalidArgumentException('ExclusiveMaximum must be greater than or equal to exclusiveMinimum.');
|
||||
}
|
||||
|
||||
if (\is_int($minItems)) {
|
||||
|
||||
@@ -26,11 +26,11 @@ use Symfony\AI\Platform\Contract\JsonSchema\Subject\ObjectSubject;
|
||||
* pattern?: string,
|
||||
* minLength?: int,
|
||||
* maxLength?: int,
|
||||
* minimum?: int,
|
||||
* maximum?: int,
|
||||
* multipleOf?: int,
|
||||
* exclusiveMinimum?: int,
|
||||
* exclusiveMaximum?: int,
|
||||
* minimum?: int|float,
|
||||
* maximum?: int|float,
|
||||
* multipleOf?: int|float,
|
||||
* exclusiveMinimum?: int|float,
|
||||
* exclusiveMaximum?: int|float,
|
||||
* minItems?: int,
|
||||
* maxItems?: int,
|
||||
* uniqueItems?: bool,
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\AI\Platform\Tests\Contract\JsonSchema\Attribute;
|
||||
|
||||
use PHPUnit\Framework\Attributes\TestWith;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With;
|
||||
use Symfony\AI\Platform\Exception\InvalidArgumentException;
|
||||
@@ -87,19 +88,15 @@ final class ToolParameterTest extends TestCase
|
||||
new With(minLength: 10, maxLength: 5);
|
||||
}
|
||||
|
||||
public function testValidMinimum()
|
||||
#[TestWith([0], 'zero')]
|
||||
#[TestWith([1.5], 'positive')]
|
||||
#[TestWith([-1], 'negative')]
|
||||
public function testValidMinimum(int|float $minimum)
|
||||
{
|
||||
$minimum = 0;
|
||||
$toolParameter = new With(minimum: $minimum);
|
||||
$this->assertSame($minimum, $toolParameter->minimum);
|
||||
}
|
||||
|
||||
public function testInvalidMinimumNegative()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
new With(minimum: -1);
|
||||
}
|
||||
|
||||
public function testValidMultipleOf()
|
||||
{
|
||||
$multipleOf = 5;
|
||||
|
||||
Reference in New Issue
Block a user