Class: Kharon::Validator
- Inherits:
-
Object
- Object
- Kharon::Validator
- Defined in:
- lib/validator.rb
Overview
The validator is the main class of Kharon, it validates a hash given a structure.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#datas ⇒ Object
readonly
The datas to filter, they shouldn’t be modified to guarantee their integrity.
-
#filtered ⇒ Object
The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
Instance Method Summary collapse
-
#any(key, options = {}) ⇒ Object
Doesn’t check the type of the key and let it pass without modification.
-
#array(key, options = {}) ⇒ Object
Checks if the given key is an array or not.
-
#boolean(key, options = {}) ⇒ Object
Checks if the given key is a boolean or not.
-
#date(key, options = {}) ⇒ Object
Checks if the given key is a date or not.
-
#datetime(key, options = {}) ⇒ Object
Checks if the given key is a datetime or not.
-
#hash(key, options = {}) ⇒ Object
Checks if the given key is a hash or not.
-
#initialize(datas) ⇒ Validator
constructor
Constructor of the classe, receiving the datas to validate and filter.
-
#integer(key, options = {}) ⇒ Object
Checks if the given key is an integer or not.
-
#numeric(key, options = {}) ⇒ Object
Checks if the given key is a numeric or not.
-
#ssid(key, options = {}) ⇒ Object
Checks if the given key is a SSID for a MongoDB object or not.
-
#text(key, options = {}) ⇒ Object
Checks if the given key is a not-empty string or not.
Constructor Details
#initialize(datas) ⇒ Validator
Constructor of the classe, receiving the datas to validate and filter.
18 19 20 21 |
# File 'lib/validator.rb', line 18 def initialize(datas) @datas = datas @filtered = Hash.new end |
Instance Attribute Details
#datas ⇒ Object (readonly)
Returns The datas to filter, they shouldn’t be modified to guarantee their integrity.
|
# File 'lib/validator.rb', line 6
|
#filtered ⇒ Object
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.
58 59 60 61 |
# File 'lib/validator.rb', line 58 def any(key, = {}) before_all(key, ) store(key, ->(item){item}, ) end |
#array(key, options = {}) ⇒ Object
Checks if the given key is an array or not.
88 89 90 91 |
# File 'lib/validator.rb', line 88 def array(key, = {}) before_all(key, ) is_typed?(key, Array) ? store_array(key, ->(item){item.to_a}, ) : raise_type_error(key, "Array") end |
#boolean(key, options = {}) ⇒ Object
Checks if the given key is a boolean or not.
108 109 110 111 |
# File 'lib/validator.rb', line 108 def boolean(key, = {}) before_all(key, ) match?(key, /(true)|(false)/) ? store(key, ->(item){to_boolean(item)}, ) : raise_type_error(key, "Numeric") end |
#date(key, options = {}) ⇒ Object
Checks if the given key is a date or not.
78 79 80 81 |
# File 'lib/validator.rb', line 78 def date(key, = {}) before_all(key, ) begin; store(key, ->(item){Date.parse(item.to_s)}, ); rescue; raise_type_error(key, "Date"); end end |
#datetime(key, options = {}) ⇒ Object
Checks if the given key is a datetime or not.
68 69 70 71 |
# File 'lib/validator.rb', line 68 def datetime(key, = {}) before_all(key, ) begin; store(key, ->(item){DateTime.parse(item.to_s)} , ); rescue; raise_type_error(key, "DateTime"); end end |
#hash(key, options = {}) ⇒ Object
Checks if the given key is a hash or not.
98 99 100 101 |
# File 'lib/validator.rb', line 98 def hash(key, = {}) before_all(key, ) is_typed?(key, Hash) ? store_hash(key, ->(item){Hash.try_convert(item)}, ) : raise_type_error(key, "Hash") end |
#integer(key, options = {}) ⇒ Object
Checks if the given key is an integer or not.
28 29 30 31 |
# File 'lib/validator.rb', line 28 def integer(key, = {}) before_all(key, ) match?(key, /\A\d+\Z/) ? store_numeric(key, ->(item){item.to_i}, ) : raise_type_error(key, "Integer") end |
#numeric(key, options = {}) ⇒ Object
Checks if the given key is a numeric or not.
38 39 40 41 |
# File 'lib/validator.rb', line 38 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 |
#ssid(key, options = {}) ⇒ Object
Checks if the given key is a SSID for a MongoDB object or not.
118 119 120 121 |
# File 'lib/validator.rb', line 118 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 |
#text(key, options = {}) ⇒ Object
Checks if the given key is a not-empty string or not.
48 49 50 51 |
# File 'lib/validator.rb', line 48 def text(key, = {}) before_all(key, ) is_typed?(key, String) ? store_text(key, ->(item){item.to_s}, ) : raise_type_error(key, "String") end |