Sha256: ac062261948d9d5b3ce495187934a70d2ad6c555027afeb56c2c7c9c15fef9d6

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# Validates values of an hash recursively
# output is an hash with coerced values
# errors is a Compel::Errors

# This is not beauty,
# but it's tested and working.
module Compel
  module Validators

    class HashValidator < Base

      def initialize(input, schema)
        super
        @output = Hashie::Mash.new
        @errors = Errors.new

        @keys_schemas = schema.options[:keys]
      end

      def validate
        @keys_schemas.keys.each do |param_name|

          if (@input[param_name].is_a?(Hash))
            hash_validator = \
              HashValidator.new(@input[param_name], @keys_schemas[param_name])
                .validate

            @errors.add(param_name, hash_validator.errors)
            @output[param_name] = hash_validator.output
          end

          type_validator = \
            TypeValidator.new(@output[param_name].nil? ? @input[param_name] : @output[param_name], @keys_schemas[param_name])
              .validate

          if !type_validator.output.nil?
            @output[param_name] = type_validator.output
          end

          if type_validator.errors
            @errors.add(param_name, type_validator.errors)
          end
        end

        self
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
compel-0.2.0 lib/compel/validators/hash_validator.rb