Class: Membrane::Schemas::Record::KeyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/membrane/schemas/record.rb

Instance Method Summary collapse

Constructor Details

#initialize(optional_keys, schemas, strict_checking, object) ⇒ KeyValidator

Returns a new instance of KeyValidator.



35
36
37
38
39
40
# File 'lib/membrane/schemas/record.rb', line 35

def initialize(optional_keys, schemas, strict_checking, object)
  @optional_keys = optional_keys
  @schemas = schemas
  @strict_checking = strict_checking
  @object = object
end

Instance Method Details

#validateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/membrane/schemas/record.rb', line 42

def validate
  key_errors = {}
  schema_keys = []
  @schemas.each do |k, schema|
    if @object.has_key?(k)
      schema_keys << k
      begin
        schema.validate(@object[k])
      rescue Membrane::SchemaValidationError => e
        key_errors[k] = e.to_s
      end
    elsif !@optional_keys.include?(k)
      key_errors[k] = "Missing key"
    end
  end

  key_errors.merge!(validate_extra_keys(@object.keys - schema_keys)) if @strict_checking

  fail!(key_errors) if key_errors.size > 0
end