This discussion is about proposing the addition of a native Rails validator that automatically checks integer column value limits based on the database schema, preventing overflow errors for integer attributes

Problem Explanation

In Rails, developers often need to ensure that values assigned to integer columns do not exceed the database’s column limit (such as int or bigint types). While we can manually validate these values, it is often cumbersome to check each column’s limit individually, especially when we need to update or change the database schema.

Current Situation

  • Manual Validation: In the current Rails ecosystem, the validation of integer values against column limits must be manually coded. For example, developers need to hard-code the max integer values in the model or use a custom solution like MAX_INT_VALUE for validation, which can quickly become inconsistent or error-prone.
  • Database-Driven Validation: The column limits (such as 4 bytes for an integer) are already defined in the database schema, but Rails lacks a built-in mechanism to automatically fetch and apply those limits in validations.

This leads to potential overflow errors if a user inputs a value larger than what the database column can hold, and it creates redundancy and inconsistency in how validation logic is handled.


Proposed Solution

The proposed solution is to introduce a native Rails validator that checks the values of integer fields based on their defined size in the database schema. The validator would automatically use the column’s limit to determine the maximum allowable value and ensure the input is within the allowed range.

Key Features of the Validator

  1. Automatic Limit Retrieval: The validator would use the limit attribute of the column from the schema (e.g., 4 bytes for an integer column) to calculate the maximum value.
  2. Prevention of Overflow: It would prevent values from exceeding the column’s limit and raise an error if the input exceeds the allowable range.
  3. Minimal Developer Effort: Developers would simply need to call the validator in the model, e.g., validates :field, max_db_column_value: true, without worrying about manually fetching column limits or hardcoding max values.
  4. Database-Driven Validation: Since column limits are stored in the database schema, this approach would be more in sync with the underlying database structure and prevent hardcoding inconsistencies.

Why This Is Important

  • Consistency: By tying the validation directly to the column’s definition in the schema, this feature helps ensure consistency between the database schema and the model logic. This reduces the chances of a developer mistakenly allowing a value that exceeds the database limit.
  • Efficiency: It eliminates the need for developers to manually track and apply limits for each integer column, which simplifies the codebase and reduces the chances of human error.
  • Compatibility: This approach would work seamlessly with different database systems (e.g., MySQL, PostgreSQL) and their respective integer types, ensuring that Rails automatically adapts to the appropriate integer size.

Example Use Case

Model Example:

class Product < ApplicationRecord
  validates :quantity,
            max_db_column_value: true
end

In this case, the validator would automatically determine the maximum allowable value for the quantity field based on its column definition in the database. No hardcoding of maximum values is required.


Next Steps

  • Feedback and Discussion: I would like to open the floor for feedback from the Rails core team and the community. Specifically, I’m interested in hearing thoughts on:
    • Whether this solution aligns with Rails’ current validation strategy.
    • Potential concerns with database portability across different DBMS systems.
    • Any improvements or alternatives that could make the solution more robust or flexible.
  • Implementation: If the idea is well-received, I would be happy to proceed with implementing the validator and submitting a pull request for review.

This sounds similar to the old schema_validations gem although last time I tried to use that it was not fully working. It does much more than you are considering though (string length checks, null checks, etc).