Skip to content

Boolean schemas (true/false) not supported for properties #118

@sergiobayona

Description

@sergiobayona

Summary

JSON Schema supports boolean schemas where true means "any value is valid" and false means "no value is valid" (forbidden property). EasyTalk doesn't support this pattern.

Expected Behavior

class MyModel
  include EasyTalk::Model
  
  define_schema do
    property :anything_goes, true   # Any value allowed
    property :forbidden, false      # Property must not exist
  end
end

MyModel.json_schema
# => {
#   "properties": {
#     "anything_goes": true,
#     "forbidden": false
#   }
# }

Actual Behavior

property :anything_goes, true
# => {"type": "boolean"}  # Wrong! Interprets `true` as TrueClass type

property :forbidden, false
# => UnknownTypeError: Unknown type 'false' for property 'forbidden'

JSON Schema Semantics

From the JSON Schema specification:

  • true: Equivalent to {} - validates any instance successfully
  • false: Equivalent to {"not": {}} - fails validation for any instance

Use Cases

  1. Forbidden properties pattern: Reject objects containing a specific key

    {
      "properties": { "bar": false },
      "additionalProperties": true
    }

    This schema allows any properties except bar.

  2. Allow-anything pattern: Accept any value for a property

    {
      "properties": { "metadata": true }
    }

Root Cause

  1. TrueClass is registered in lib/easy_talk/builders/registry.rb:114 and maps to BooleanBuilder, which outputs {"type": "boolean"} instead of the literal true

  2. FalseClass is not registered at all, causing UnknownTypeError

  3. The Property class has no special handling to detect boolean schema literals vs boolean types

Proposed Solution

Add special handling in Property#build to detect when the type is literally true or false and output them directly as boolean schemas:

def build
  # Handle boolean schemas (JSON Schema true/false)
  return true if type.equal?(true)
  return false if type.equal?(false)
  
  # ... rest of existing logic
end

Note: Use equal? to distinguish between the literal true/false values and TrueClass/FalseClass as type references.

Reference

JSON Schema Test Suite - "properties with boolean schema":

{
  "description": "properties with boolean schema",
  "schema": {
    "properties": {
      "foo": true,
      "bar": false
    }
  },
  "tests": [
    { "description": "no property present is valid", "data": {}, "valid": true },
    { "description": "only 'true' property present is valid", "data": {"foo": 1}, "valid": true },
    { "description": "only 'false' property present is invalid", "data": {"bar": 2}, "valid": false },
    { "description": "both properties present is invalid", "data": {"foo": 1, "bar": 2}, "valid": false }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    json schema complianceIssues related to JSON Schema specification compliancepriority: mediumJSON Schema compliance and testing improvements

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions