Skip to content

[252]: add DB type range validation rule - #253

Merged
DenTray merged 50 commits into
masterfrom
252-add-db-type-range-validation-rule
Jun 15, 2026
Merged

[252]: add DB type range validation rule#253
DenTray merged 50 commits into
masterfrom
252-add-db-type-range-validation-rule

Conversation

@vitgrams

Copy link
Copy Markdown
Contributor

refs: #252

@vitgrams vitgrams self-assigned this Mar 11, 2026
@vitgrams
vitgrams force-pushed the 252-add-db-type-range-validation-rule branch 5 times, most recently from 9450961 to b21cb0e Compare March 11, 2026 18:38
@vitgrams
vitgrams requested a review from yburlakov March 11, 2026 18:42
@vitgrams vitgrams removed their assignment Mar 11, 2026
@vitgrams

Copy link
Copy Markdown
Contributor Author

@yburlakov Please review these changes

Comment thread src/Rules/DbTypeRangeRule.php Outdated
@yburlakov yburlakov assigned vitgrams and unassigned yburlakov Mar 12, 2026
@vitgrams vitgrams assigned yburlakov and unassigned vitgrams Mar 12, 2026
@vitgrams

Copy link
Copy Markdown
Contributor Author

@yburlakov Fixed review remarks

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Laravel validation rule (db_type_range) intended to validate request values against predefined min/max ranges derived from DB column data types (with a default PostgreSQL type range map), and registers it for string-rule usage via the package service provider.

Changes:

  • Introduces DbTypeRangeRule (implements ValidationRule) that validates either numeric ranges or string length ranges based on a type→range map.
  • Adds a default PostgreSQL type range map via PostgresDatabaseTypeEnum implementing DatabaseTypeRangesContract, bound in HelpersServiceProvider.
  • Extends ValidatorTest with new coverage for the rule (pass/fail cases, missing/unknown type handling, object syntax).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/ValidatorTest.php Adds tests for the new db_type_range validator and object rule usage.
src/Rules/DbTypeRangeRule.php Implements the core range validation logic using a container-resolved ranges provider.
src/HelpersServiceProvider.php Registers the db_type_range string rule and binds the default ranges provider.
src/Enums/PostgresDatabaseTypeEnum.php Defines the default Postgres type→range mapping.
src/Contracts/DatabaseTypeRangesContract.php Adds the contract for providing type→range mappings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Rules/DbTypeRangeRule.php Outdated
Comment thread src/Rules/DbTypeRangeRule.php Outdated
Comment thread src/HelpersServiceProvider.php
Comment thread tests/ValidatorTest.php
Comment thread src/Enums/PostgresDatabaseTypeEnum.php Outdated
Comment thread src/Rules/DBTypeRangeRule.php
@yburlakov yburlakov assigned vitgrams and unassigned DenTray Mar 24, 2026
vitgrams added a commit that referenced this pull request Mar 24, 2026
vitgrams added a commit that referenced this pull request Mar 24, 2026
vitgrams added a commit that referenced this pull request Mar 24, 2026
@vitgrams
vitgrams force-pushed the 252-add-db-type-range-validation-rule branch from 06a9d24 to 190e71e Compare March 25, 2026 07:56
@vitgrams
vitgrams force-pushed the 252-add-db-type-range-validation-rule branch from d1ce30a to dea6e14 Compare May 28, 2026 09:25

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dea6e14fd1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Rules/DBTypeRangeRule.php Outdated

protected function validateInteger(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
{
if (!preg_match(self::INTEGER_PATTERN, (string) $value)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard non-scalar integer inputs before casting

When an integer-backed rule receives a non-scalar value, such as a request array for db_type_range:integer, this cast runs before validation can fail normally. (string) ['foo'] emits an Array to string conversion warning, which Laravel/PHPUnit error handling commonly turns into an exception, and objects without __toString() throw immediately; those inputs should produce the integer validation error instead of crashing the validator.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point! fixed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da9c0e9d1f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Rules/DBTypeRangeRule.php Outdated

protected function validateInteger(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
{
if (!is_scalar($value) || !preg_match(self::INTEGER_PATTERN, (string) $value)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject booleans before integer range checks

When JSON/request data supplies true for an integer-backed type such as db_type_range:integer, this condition treats the boolean as scalar and then casts it to the string '1', so the range check passes. That allows a boolean value through a rule whose error text and DB type both require an integer, and PostgreSQL integer columns will reject a boolean parameter instead of accepting it as 1; booleans should be excluded before casting.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\

@vitgrams vitgrams assigned DenTray and unassigned vitgrams May 29, 2026
Comment thread src/Rules/DBTypeRangeRule.php Outdated
Comment on lines +37 to +42
match (true) {
$this->resolver->isTypeCategory(DBTypeCategoryEnum::Integer, $this->type) => $this->validateInteger($attribute, $value, $min, $max, $fail),
$this->resolver->isTypeCategory(DBTypeCategoryEnum::Float, $this->type) => $this->validateFloat($attribute, $value, $min, $max, $fail),
$this->resolver->isTypeCategory(DBTypeCategoryEnum::String, $this->type) => $this->validateString($attribute, $value, $max, $fail),
default => null,
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match (true) {
$this->resolver->isTypeCategory(DBTypeCategoryEnum::Integer, $this->type) => $this->validateInteger($attribute, $value, $min, $max, $fail),
$this->resolver->isTypeCategory(DBTypeCategoryEnum::Float, $this->type) => $this->validateFloat($attribute, $value, $min, $max, $fail),
$this->resolver->isTypeCategory(DBTypeCategoryEnum::String, $this->type) => $this->validateString($attribute, $value, $max, $fail),
default => null,
};
match ($this->resolver->getTypeCategory($this->type)) {
DBTypeCategoryEnum::Integer => $this->validateInteger($attribute, $value, $min, $max, $fail),
DBTypeCategoryEnum::Float => $this->validateFloat($attribute, $value, $min, $max, $fail),
DBTypeCategoryEnum::String => $this->validateString($attribute, $value, $max, $fail),
default => null,
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

Comment thread src/Enums/DBTypeCategoryEnum.php Outdated
Comment on lines +5 to +9
enum DBTypeCategoryEnum: string
{
case Integer = 'int';
case Float = 'float';
case String = 'string';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum DBTypeCategoryEnum: string
{
case Integer = 'int';
case Float = 'float';
case String = 'string';
enum DBTypeCategoryEnum
{
case Integer;
case Float;
case String;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

@DenTray DenTray assigned vitgrams and unassigned DenTray Jun 9, 2026
…ry(), removed backed enum string values;

refs: #252

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c1b2caf25

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

match ($this->resolver->getTypeCategory($this->type)) {
DBTypeCategoryEnum::Integer => $this->validateInteger($attribute, $value, $min, $max, $fail),
DBTypeCategoryEnum::Float => $this->validateFloat($attribute, $value, $min, $max, $fail),
DBTypeCategoryEnum::String => $this->validateString($attribute, $value, $max, $fail),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Enforce lower bounds for string ranges

When a custom resolver categorizes a type as String with a non-zero lower bound, this dispatch drops $min and validateString() only checks the maximum length, so values that are shorter than the declared range pass validation. Fresh evidence in the current code is that the string branch still passes only $max, even though getRange() returns both bounds and the custom resolver extension point exposes string ranges.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For DB type validation, particularly for strings, using 0 as the lower bound makes sense. If the value is something other than zero, for example 5, that's more of a business rule than a type constraint. Laravel already provides the min validation rule for those kinds of requirements.

@vitgrams vitgrams assigned DenTray and unassigned vitgrams Jun 13, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread src/Support/PostgresDBTypeResolver.php Outdated
Comment thread src/Contracts/DBTypeResolverContract.php Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d05e579d3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


private const array RANGES = [
self::SMALLINT => [-32768, 32767],
self::INTEGER => [-2147483648, 2147483647],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Register PostgreSQL integer aliases

When callers use PostgreSQL's documented integer spellings such as int (or schema/introspection names like int2, int4, and int8), hasType() reports them as unknown because only integer, smallint, and bigint are keyed here. PostgreSQL documents these as aliases/extensions for the same integer types (https://www.postgresql.org/docs/current/datatype-numeric.html), so db_type_range:int currently throws instead of applying the existing integer range; add those aliases to the range map and category match.

Useful? React with 👍 / 👎.

@DenTray
DenTray merged commit 1c078f5 into master Jun 15, 2026
12 checks passed
@DenTray
DenTray deleted the 252-add-db-type-range-validation-rule branch June 15, 2026 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants