Skip to content

Commit 602f0cd

Browse files
updated
1 parent 6ff2d7b commit 602f0cd

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/Database/Database.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,9 @@ protected function validateDefaultTypes(string $type, mixed $default): void
26102610
if ($defaultType !== 'integer' && $defaultType !== 'string') {
26112611
throw new DatabaseException('Default value ' . $default . ' does not match given type ' . $type);
26122612
}
2613+
if ($defaultType === 'string' && !BigIntValidator::isIntegerString($default)) {
2614+
throw new DatabaseException('Default value ' . $default . ' is not a valid integer string for type bigint');
2615+
}
26132616
break;
26142617
case self::VAR_DATETIME:
26152618
if ($defaultType !== self::VAR_STRING) {

src/Database/Validator/Attribute.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ protected function validateDefaultTypes(string $type, mixed $default): void
540540
$this->message = 'Default value ' . $default . ' does not match given type ' . $type;
541541
throw new DatabaseException($this->message);
542542
}
543+
if ($defaultType === 'string' && !BigInt::isIntegerString($default)) {
544+
$this->message = 'Default value ' . $default . ' is not a valid integer string for type bigint';
545+
throw new DatabaseException($this->message);
546+
}
543547
break;
544548
case Database::VAR_DATETIME:
545549
if ($defaultType !== Database::VAR_STRING) {

tests/unit/Validator/AttributeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,56 @@ public function testValidIntegerWithDefaultValue(): void
10401040
$this->assertTrue($validator->isValid($attribute));
10411041
}
10421042

1043+
public function testInvalidBigIntDefaultValueTypeStringNotNumeric(): void
1044+
{
1045+
$validator = new Attribute(
1046+
attributes: [],
1047+
maxStringLength: 16777216,
1048+
maxVarcharLength: 65535,
1049+
maxIntLength: PHP_INT_MAX,
1050+
);
1051+
1052+
$attribute = new Document([
1053+
'$id' => ID::custom('counter'),
1054+
'key' => 'counter',
1055+
'type' => Database::VAR_BIGINT,
1056+
'size' => 0,
1057+
'required' => false,
1058+
'default' => 'not_a_bigint',
1059+
'signed' => true,
1060+
'array' => false,
1061+
'filters' => [],
1062+
]);
1063+
1064+
$this->expectException(DatabaseException::class);
1065+
$this->expectExceptionMessage('Default value not_a_bigint is not a valid integer string for type bigint');
1066+
$validator->isValid($attribute);
1067+
}
1068+
1069+
public function testValidBigIntDefaultValueTypeStringNumeric(): void
1070+
{
1071+
$validator = new Attribute(
1072+
attributes: [],
1073+
maxStringLength: 16777216,
1074+
maxVarcharLength: 65535,
1075+
maxIntLength: PHP_INT_MAX,
1076+
);
1077+
1078+
$attribute = new Document([
1079+
'$id' => ID::custom('counter'),
1080+
'key' => 'counter',
1081+
'type' => Database::VAR_BIGINT,
1082+
'size' => 0,
1083+
'required' => false,
1084+
'default' => '123',
1085+
'signed' => true,
1086+
'array' => false,
1087+
'filters' => [],
1088+
]);
1089+
1090+
$this->assertTrue($validator->isValid($attribute));
1091+
}
1092+
10431093
public function testValidFloatWithDefaultValue(): void
10441094
{
10451095
$validator = new Attribute(

0 commit comments

Comments
 (0)