Class: HashSchema::OrSchema

Inherits:
Schema
  • Object
show all
Defined in:
lib/hash_schema.rb

Instance Attribute Summary

Attributes inherited from Schema

#chain

Instance Method Summary collapse

Methods inherited from Schema

#interpret, #pretty_validate

Constructor Details

#initialize(*schemas) ⇒ OrSchema

Returns a new instance of OrSchema.



84
85
86
# File 'lib/hash_schema.rb', line 84

def initialize(*schemas)
  @chain = schemas
end

Instance Method Details

#expectationObject



98
99
100
101
# File 'lib/hash_schema.rb', line 98

def expectation
  *names, name = chain.map(&:expectation)
  names.join(', ') << " or #{name}"
end

#validate(data) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/hash_schema.rb', line 88

def validate(data)
  chain.each do |schema|
    if data.is_a?(Array) && schema.is_a?(ArraySchema) || data.is_a?(Hash) && schema.is_a?(HashSchema)
      return schema.validate(data)
    end
    return if schema.validate(data).nil?
  end
  error(data)
end