Class: Kharon::Validator

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

Overview

The validator is the main class of Kharon, it validates a hash given a structure.

Author:

Direct Known Subclasses

CoolValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datas) ⇒ Validator

Constructor of the classe, receiving the datas to validate and filter.

Examples:

create a new instance of validator.

@validator = Kharon::Validator.new({key: "value"})

Parameters:

  • datas (Hash)

    the datas to validate in the validator.



18
19
20
21
# File 'lib/validator.rb', line 18

def initialize(datas)
  @datas    = datas
  @filtered = Hash.new
end

Instance Attribute Details

#datasObject (readonly)

Returns The datas to filter, they shouldn’t be modified to guarantee their integrity.

Returns:

  • The datas to filter, they shouldn’t be modified to guarantee their integrity.



# File 'lib/validator.rb', line 6

#filteredObject

Returns The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.

Returns:

  • The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.



# File 'lib/validator.rb', line 10

Instance Method Details

#any(key, options = {}) ⇒ Object

Doesn’t check the type of the key and let it pass without modification.

Examples:

Just checks if the key is in the hash.

@validator.any(:a_key, required: true)

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



58
59
60
61
# File 'lib/validator.rb', line 58

def any(key, options = {})
  before_all(key, options)
  store(key, ->(item){item}, options)
end

#array(key, options = {}) ⇒ Object

Checks if the given key is an array or not.

Examples:

Validates a key so it has to be an array, and checks if it has some values in it.

@validator.date(:an_array, contains?: ["first", "second"])

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



88
89
90
91
# File 'lib/validator.rb', line 88

def array(key, options = {})
  before_all(key, options)
  is_typed?(key, Array) ? store_array(key, ->(item){item.to_a}, options) : raise_type_error(key, "Array")
end

#boolean(key, options = {}) ⇒ Object

Checks if the given key is a boolean or not.

Examples:

Validates a key so it has to be a boolean.

@validator.boolean(:a_boolean)

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



108
109
110
111
# File 'lib/validator.rb', line 108

def boolean(key, options = {})
  before_all(key, options)
  match?(key, /(true)|(false)/) ? store(key, ->(item){to_boolean(item)}, options) : raise_type_error(key, "Numeric")
end

#date(key, options = {}) ⇒ Object

Checks if the given key is a date or not.

Examples:

Validates a key so it has to be a date, and depends on another key.

@validator.date(:a_date, dependency: :another_key)

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



78
79
80
81
# File 'lib/validator.rb', line 78

def date(key, options = {})
  before_all(key, options)
  begin; store(key, ->(item){Date.parse(item.to_s)}, options); rescue; raise_type_error(key, "Date"); end
end

#datetime(key, options = {}) ⇒ Object

Checks if the given key is a datetime or not.

Examples:

Validates a key so it has to be a datetime, and depends on two other keys.

@validator.datetime(:a_datetime, dependencies: [:another_key, :a_third_key])

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



68
69
70
71
# File 'lib/validator.rb', line 68

def datetime(key, options = {})
  before_all(key, options)
  begin; store(key, ->(item){DateTime.parse(item.to_s)} , options); rescue; raise_type_error(key, "DateTime"); end
end

#hash(key, options = {}) ⇒ Object

Checks if the given key is a hash or not.

Examples:

Validates a key so it has to be a hash, and checks if it has some keys.

@validator.date(:a_hash, has_keys: [:first, :second])

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



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

def hash(key, options = {})
  before_all(key, options)
  is_typed?(key, Hash) ? store_hash(key, ->(item){Hash.try_convert(item)}, options) : raise_type_error(key, "Hash")
end

#integer(key, options = {}) ⇒ Object

Checks if the given key is an integer or not.

Examples:

Validates a key so it has to be an integer superior or equal to 2.

@validator.integer(:an_integer_key, min: 2)

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



28
29
30
31
# File 'lib/validator.rb', line 28

def integer(key, options = {})
  before_all(key, options)
  match?(key, /\A\d+\Z/) ? store_numeric(key, ->(item){item.to_i}, options) : raise_type_error(key, "Integer")
end

#numeric(key, options = {}) ⇒ Object

Checks if the given key is a numeric or not.

Examples:

Validates a key so it has to be a numeric, is required and is between 2 and 5.5.

@validator.numeric(:a_numeric_key, required: true, between: [2, 5.5])

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



38
39
40
41
# File 'lib/validator.rb', line 38

def numeric(key, options = {})
  before_all(key, options)
  match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store_decimal(key, ->(item){item.to_s.sub(/,/, ".").to_f}, options) : raise_type_error(key, "Numeric")
end

#ssid(key, options = {}) ⇒ Object

Checks if the given key is a SSID for a MongoDB object or not.

Examples:

Validates a key so it has to be a MongoDB SSID.

@validator.ssid(:a_ssid)

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



118
119
120
121
# File 'lib/validator.rb', line 118

def ssid(key, options = {})
  before_all(key, options)
  match?(key, /^[0-9a-fA-F]{24}$/) ? store(key, ->(item){BSON::ObjectId.from_string(item.to_s)}, options) : raise_type_error(key, "Moped::BSON::ObjectId")
end

#text(key, options = {}) ⇒ Object

Checks if the given key is a not-empty string or not.

Examples:

Validates a key so it has to be a string, and seems like and email address (not sure of the regular expression though).

@validator.text(:an_email, regex: "[a-zA-Z]+@[a-zA-Z]+\.[a-zA-Z]{2-4}")

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



48
49
50
51
# File 'lib/validator.rb', line 48

def text(key, options = {})
  before_all(key, options)
  is_typed?(key, String) ? store_text(key, ->(item){item.to_s}, options) : raise_type_error(key, "String")
end