Class: Expectant::BoundSchema
- Inherits:
-
Object
- Object
- Expectant::BoundSchema
- Defined in:
- lib/expectant/bound_schema.rb
Overview
Instance-bound schema wrapper that provides validation with instance context
Instance Method Summary collapse
- #contract ⇒ Object
-
#initialize(instance, schema) ⇒ BoundSchema
constructor
A new instance of BoundSchema.
- #keys ⇒ Object
- #validate(data, context: {}) ⇒ Object
Constructor Details
#initialize(instance, schema) ⇒ BoundSchema
6 7 8 9 |
# File 'lib/expectant/bound_schema.rb', line 6 def initialize(instance, schema) @instance = instance @schema = schema end |
Instance Method Details
#contract ⇒ Object
36 37 38 |
# File 'lib/expectant/bound_schema.rb', line 36 def contract @schema.contract end |
#keys ⇒ Object
32 33 34 |
# File 'lib/expectant/bound_schema.rb', line 32 def keys @schema.keys end |
#validate(data, context: {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/expectant/bound_schema.rb', line 11 def validate(data, context: {}) # Apply proc defaults before validation data = apply_defaults(data) # Create contract instance with context, then validate contract_class = @schema.contract contract = contract_class.new(instance: @instance, context: context) result = contract.call(data) # If validation failed, apply fallbacks for fields with errors and retry if !result.success? data_with_fallbacks = apply_fallbacks(data, result) if data_with_fallbacks != data # Re-validate with fallback values result = contract.call(data_with_fallbacks) end end result end |