-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Labels
priority: criticalMust fix - bugs breaking core functionalityMust fix - bugs breaking core functionality
Description
Summary
There is a semantic mismatch between JSON Schema validation and ActiveModel validation for required string properties:
- JSON Schema:
type: "string"allows empty strings"" - ActiveModel:
presence: truerejects empty strings
This causes the validations to disagree on whether data is valid.
Steps to Reproduce
class User
include EasyTalk::Model
define_schema do
property :name, String
end
end
user = User.new(name: "")
# JSON Schema validation (using json_schemer)
schema = User.json_schema
# => { "type" => "object", "properties" => { "name" => { "type" => "string" } }, "required" => ["name"] }
schemer = JSONSchemer.schema(schema)
schemer.valid?({ "name" => "" })
# => true (empty string is valid for type: "string")
# ActiveModel validation
user.valid?
# => false (presence: true rejects empty strings)
user.errors[:name]
# => ["can't be blank"]Expected Behavior
Validations should be consistent. Either:
- JSON Schema should include
minLength: 1for required string properties, OR - ActiveModel should allow empty strings for type validation (only using
presencewhere explicitly requested)
Analysis
This is a semantic difference rather than a bug. Both behaviors are technically correct for their respective systems:
- JSON Schema's
requiredmeans "key must be present" not "value must be non-empty" - ActiveModel's
presence: truemeans "value must not be blank"
Options to Consider
- Add
minLength: 1to JSON Schema for required strings (breaking change for schema consumers) - Make presence validation opt-in with a constraint like
presence: trueorblank: false - Document the difference and accept that the two validation systems have different semantics
- Add configuration option to control this behavior
Verification
This gap was identified using the new have_matching_validations_for RSpec matcher:
# This test correctly detects the mismatch
expect(User).not_to have_matching_validations_for(name: '')Impact
- APIs that validate against JSON Schema may accept empty strings that Rails controllers reject
- This could cause confusion when debugging validation issues
- LLM function calling responses may include empty strings that fail Rails validation
🤖 Generated with Claude Code
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
priority: criticalMust fix - bugs breaking core functionalityMust fix - bugs breaking core functionality