Skip to content

Add strict_types configuration option for JSON Schema type compliance #137

@sergiobayona

Description

@sergiobayona

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)
end

Per-Model Configuration

class User
  include EasyTalk::Model
  define_schema do
    strict_types true  # override global setting for this model
    property :age, Integer
  end
end

Behavior

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 false for 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=True mode

Related

  • JSON Schema Test Suite: type.json tests
  • Test added in spec/easy_talk/schema_validation_spec.rb documenting current coercion behavior

Labels

enhancement, json-schema-compliance

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: highHigh value features or important fixes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions