Class: Kharon::Validator

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

Overview

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

Author:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Validator) initialize(datas)

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.



23
24
25
26
27
# File 'lib/kharon/validator.rb', line 23

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

Instance Attribute Details

- (Hash) datas (readonly)

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

Returns:

  • (Hash)

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



# File 'lib/kharon/validator.rb', line 7

- (Hash) filtered

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

Returns:

  • (Hash)

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



# File 'lib/kharon/validator.rb', line 11

- (Object) handler

Returns the error handler given to this instance of the validator.

Returns:

  • (Object)

    the error handler given to this instance of the validator.



# File 'lib/kharon/validator.rb', line 15

Instance Method Details

- (Object) any(key, options = {})

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).



64
65
66
67
# File 'lib/kharon/validator.rb', line 64

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

- (Object) array(key, options = {})

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).



94
95
96
97
# File 'lib/kharon/validator.rb', line 94

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

- (Object) boolean(key, options = {})

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).



114
115
116
117
# File 'lib/kharon/validator.rb', line 114

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

- (Object) box(key, options = {})

Checks if the given key is a box (geofences) or not. A box is composed of four numbers (positive or negative, decimal or not) separed by commas.

Examples:

Validates a key so it has to be a box.

@validator.box(:a_box)

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).



134
135
136
137
# File 'lib/kharon/validator.rb', line 134

def box(key, options = {})
  before_all(key, options)
  match?(key, /^(?:[+-]?\d{1,3}(?:\.\d{1,7})?,?){4}$/) ? store_box(key, options) : raise_type_error(key, "Box")
end

- (Object) date(key, options = {})

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).



84
85
86
87
# File 'lib/kharon/validator.rb', line 84

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

- (Object) datetime(key, options = {})

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).



74
75
76
77
# File 'lib/kharon/validator.rb', line 74

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

- (Object) email(key, options = {})

Checks if the given key is an email or not.

Examples:

Validates a key so it has to be an email.

@validator.email(:email)

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).



144
145
146
147
# File 'lib/kharon/validator.rb', line 144

def email(key, options = {})
  before_all(key, options)
  match?(key, /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/) ? store(key, ->(item){item}, options) : raise_type_error(key, "Email")
end

- (Object) hash(key, options = {})

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).



104
105
106
107
# File 'lib/kharon/validator.rb', line 104

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

- (Object) integer(key, options = {})

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).



34
35
36
37
# File 'lib/kharon/validator.rb', line 34

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

- (Object) numeric(key, options = {})

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).



44
45
46
47
# File 'lib/kharon/validator.rb', line 44

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

- (Object) ssid(key, options = {})

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).



124
125
126
127
# File 'lib/kharon/validator.rb', line 124

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

- (Object) text(key, options = {})

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).



54
55
56
57
# File 'lib/kharon/validator.rb', line 54

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