-
Notifications
You must be signed in to change notification settings - Fork 12
Description
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 successfullyfalse: Equivalent to{"not": {}}- fails validation for any instance
Use Cases
-
Forbidden properties pattern: Reject objects containing a specific key
{ "properties": { "bar": false }, "additionalProperties": true }This schema allows any properties except
bar. -
Allow-anything pattern: Accept any value for a property
{ "properties": { "metadata": true } }
Root Cause
-
TrueClassis registered inlib/easy_talk/builders/registry.rb:114and maps toBooleanBuilder, which outputs{"type": "boolean"}instead of the literaltrue -
FalseClassis not registered at all, causingUnknownTypeError -
The
Propertyclass 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
endNote: 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 }
]
}