Class: Kharon::Validator
- Inherits:
-
Object
- Object
- Kharon::Validator
- Defined in:
- lib/kharon/validator.rb
Overview
The validator is the main class of Kharon, it validates a hash given a structure.
Instance Attribute Summary (collapse)
-
- (Hash) datas
readonly
The datas to filter, they shouldn't be modified to guarantee their integrity.
-
- (Hash) filtered
The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
-
- (Object) handler
The error handler given to this instance of the validator.
Instance Method Summary (collapse)
-
- (Object) any(key, options = {})
Doesn't check the type of the key and let it pass without modification.
-
- (Object) array(key, options = {})
Checks if the given key is an array or not.
-
- (Object) boolean(key, options = {})
Checks if the given key is a boolean or not.
-
- (Object) box(key, options = {})
Checks if the given key is a box (geofences) or not.
-
- (Object) date(key, options = {})
Checks if the given key is a date or not.
-
- (Object) datetime(key, options = {})
Checks if the given key is a datetime or not.
-
- (Object) email(key, options = {})
Checks if the given key is an email or not.
-
- (Object) hash(key, options = {})
Checks if the given key is a hash or not.
-
- (Validator) initialize(datas)
constructor
Constructor of the classe, receiving the datas to validate and filter.
-
- (Object) integer(key, options = {})
Checks if the given key is an integer or not.
-
- (Object) numeric(key, options = {})
Checks if the given key is a numeric or not.
-
- (Object) ssid(key, options = {})
Checks if the given key is a SSID for a MongoDB object or not.
-
- (Object) text(key, options = {})
Checks if the given key is a not-empty string or not.
Constructor Details
- (Validator) initialize(datas)
Constructor of the classe, receiving the datas to validate and filter.
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.
|
# 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.
|
# File 'lib/kharon/validator.rb', line 11
|
- (Object) handler
Returns 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.
64 65 66 67 |
# File 'lib/kharon/validator.rb', line 64 def any(key, = {}) before_all(key, ) store(key, ->(item){item}, ) end |
- (Object) array(key, options = {})
Checks if the given key is an array or not.
94 95 96 97 |
# File 'lib/kharon/validator.rb', line 94 def array(key, = {}) before_all(key, ) is_typed?(key, Array) ? store_array(key, ->(item){item.to_a}, ) : raise_type_error(key, "Array") end |
- (Object) boolean(key, options = {})
Checks if the given key is a boolean or not.
114 115 116 117 |
# File 'lib/kharon/validator.rb', line 114 def boolean(key, = {}) before_all(key, ) match?(key, /(true)|(false)/) ? store(key, ->(item){to_boolean(item)}, ) : 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.
134 135 136 137 |
# File 'lib/kharon/validator.rb', line 134 def box(key, = {}) before_all(key, ) match?(key, /^(?:[+-]?\d{1,3}(?:\.\d{1,7})?,?){4}$/) ? store_box(key, ) : raise_type_error(key, "Box") end |
- (Object) date(key, options = {})
Checks if the given key is a date or not.
84 85 86 87 |
# File 'lib/kharon/validator.rb', line 84 def date(key, = {}) before_all(key, ) begin; store(key, ->(item){Date.parse(item.to_s)}, ); rescue; raise_type_error(key, "Date"); end end |
- (Object) datetime(key, options = {})
Checks if the given key is a datetime or not.
74 75 76 77 |
# File 'lib/kharon/validator.rb', line 74 def datetime(key, = {}) before_all(key, ) begin; store(key, ->(item){DateTime.parse(item.to_s)} , ); rescue; raise_type_error(key, "DateTime"); end end |
- (Object) email(key, options = {})
Checks if the given key is an email or not.
144 145 146 147 |
# File 'lib/kharon/validator.rb', line 144 def email(key, = {}) before_all(key, ) match?(key, /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/) ? store(key, ->(item){item}, ) : raise_type_error(key, "Email") end |
- (Object) hash(key, options = {})
Checks if the given key is a hash or not.
104 105 106 107 |
# File 'lib/kharon/validator.rb', line 104 def hash(key, = {}) before_all(key, ) is_typed?(key, Hash) ? store_hash(key, ->(item){Hash.try_convert(item)}, ) : raise_type_error(key, "Hash") end |
- (Object) integer(key, options = {})
Checks if the given key is an integer or not.
34 35 36 37 |
# File 'lib/kharon/validator.rb', line 34 def integer(key, = {}) before_all(key, ) match?(key, /\A\d+\Z/) ? store_numeric(key, ->(item){item.to_i}, ) : raise_type_error(key, "Integer") end |
- (Object) numeric(key, options = {})
Checks if the given key is a numeric or not.
44 45 46 47 |
# File 'lib/kharon/validator.rb', line 44 def numeric(key, = {}) before_all(key, ) match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store_decimal(key, ->(item){item.to_s.sub(/,/, ".").to_f}, ) : raise_type_error(key, "Numeric") end |
- (Object) ssid(key, options = {})
Checks if the given key is a SSID for a MongoDB object or not.
124 125 126 127 |
# File 'lib/kharon/validator.rb', line 124 def ssid(key, = {}) before_all(key, ) match?(key, /^[0-9a-fA-F]{24}$/) ? store(key, ->(item){BSON::ObjectId.from_string(item.to_s)}, ) : raise_type_error(key, "Moped::BSON::ObjectId") end |
- (Object) text(key, options = {})
Checks if the given key is a not-empty string or not.
54 55 56 57 |
# File 'lib/kharon/validator.rb', line 54 def text(key, = {}) before_all(key, ) is_typed?(key, String) ? store_text(key, ->(item){item.to_s}, ) : raise_type_error(key, "String") end |