-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Labels
priority: highHigh value features or important fixesHigh value features or important fixes
Description
Summary
Add a strict_types configuration option that enables strict type checking to match JSON Schema specification behavior. When enabled, type validation will reject values that don't exactly match the declared type, even if they could be coerced.
Background
Currently, EasyTalk uses ActiveModel's numericality validation which coerces strings to numbers. This means:
class User
include EasyTalk::Model
define_schema do
property :age, Integer
end
end
user = User.new(age: "30")
user.valid? # => true (string "30" is coerced to integer 30)Per JSON Schema specification, a string "30" should not be valid for type: integer - the type must match exactly.
Proposed Solution
Global Configuration
EasyTalk.configure do |config|
config.strict_types = true # default: false (backwards compatible)
endPer-Model Configuration
class User
include EasyTalk::Model
define_schema do
strict_types true # override global setting for this model
property :age, Integer
end
endBehavior
| Value | Type | strict_types: false |
strict_types: true |
|---|---|---|---|
30 |
Integer | valid | valid |
"30" |
Integer | valid (coerced) | invalid |
30.0 |
Integer | valid | valid (per JSON Schema, 30.0 is a valid integer) |
30.5 |
Integer | invalid | invalid |
"hello" |
Integer | invalid | invalid |
3.14 |
Float | valid | valid |
"3.14" |
Float | valid (coerced) | invalid |
true |
Boolean | valid | valid |
"true" |
Boolean | invalid | invalid |
Use Cases
When coercion is helpful (default):
- HTML forms submit everything as strings
- Query params are strings
- CSV imports
- Rails-style "params come as strings" workflows
When strict types matter:
- LLM function calling (OpenAI, Anthropic) - exact type contracts
- API validation where you control the input format
- Data integrity guarantees
- JSON Schema compliance testing/certification
Implementation Notes
- Default should be
falsefor backwards compatibility - Consider adding per-property override:
property :age, Integer, strict: true - Error messages should be clear: "must be an integer" vs "is not a number"
- Reference: Pydantic has similar
strict=Truemode
Related
- JSON Schema Test Suite:
type.jsontests - Test added in
spec/easy_talk/schema_validation_spec.rbdocumenting current coercion behavior
Labels
enhancement, json-schema-compliance
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
priority: highHigh value features or important fixesHigh value features or important fixes