Module: Kharon::Validate

Defined in:
lib/kharon/validate.rb

Overview

Module to include to use the #validate method in your own classes. It offers an easier way to validate datas than creating the validator from scratch.

Author:

Instance Method Summary (collapse)

Instance Method Details

- (Hash) validate(datas, &block)

Validates the datas passed as parameter with a Kharon::Validator and the given instructions.

Parameters:

  • datas (Hash)

    the parameters to validate with the given instructions.

  • block (Proc)

    the instructions to apply on the validator.

Returns:

  • (Hash)

    the validated and filtered datas.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kharon/validate.rb', line 9

def validate(datas, &block)
  begin
    validator = Kharon::Validator.new(datas)
    validator.instance_eval(&block)
    return validator.filtered
  rescue Kharon::Errors::Validation => exception
    raise exception
  rescue Exception => exception
    raise Kharon::Errors::Validation.new({type: "standard", exception: exception.class.to_s, message: exception.message, backtrace: exception.backtrace})
  end
end