Class: Compel::Validators::HashValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/compel/validators/hash_validator.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#errors, #input, #output, #schema

Instance Method Summary collapse

Methods inherited from Base

#valid?, validate

Constructor Details

#initialize(input, schema) ⇒ HashValidator

Returns a new instance of HashValidator.



11
12
13
14
15
16
# File 'lib/compel/validators/hash_validator.rb', line 11

def initialize(input, schema)
  super

  @errors = Errors.new
  @keys_schemas = schema.options[:keys][:value]
end

Instance Attribute Details

#keys_schemasObject (readonly)

Returns the value of attribute keys_schemas.



9
10
11
# File 'lib/compel/validators/hash_validator.rb', line 9

def keys_schemas
  @keys_schemas
end

Instance Method Details

#serializeObject



32
33
34
35
36
37
38
39
40
# File 'lib/compel/validators/hash_validator.rb', line 32

def serialize
  coerced = output.is_a?(Hash) ? input.merge(output) : {}

  coerced.tap do |hash|
    if !errors.empty?
      hash[:errors] = serialize_errors
    end
  end
end

#serialize_errorsObject



42
43
44
# File 'lib/compel/validators/hash_validator.rb', line 42

def serialize_errors
  errors.to_hash
end

#validateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/compel/validators/hash_validator.rb', line 18

def validate
  unless root_hash_valid?
    return self
  end

  keys_validator = \
    HashKeysValidator.validate(input, keys_schemas)

  @errors = keys_validator.errors
  @output = keys_validator.output

  self
end