Class: SBSM::Validator
- Inherits:
-
Object
- Object
- SBSM::Validator
- Defined in:
- lib/sbsm/validator.rb
Constant Summary collapse
- BOOLEAN =
[]
- DATES =
[]
- ENUMS =
{}
- EVENTS =
[]
- FILES =
[]
- HTML =
[]
- NUMERIC =
[]
- PATTERNS =
{}
- STRINGS =
[]
- URIS =
[]
- ALLOWED_TAGS =
%{a b br div font h1 h2 h3 i img li ol p pre span strong u ul}
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize ⇒ Validator
constructor
A new instance of Validator.
- #perform_validation(key, value) ⇒ Object
- #reset_errors ⇒ Object
- #set_pass(value1, value2) ⇒ Object
- #valid_values(key) ⇒ Object
- #validate(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Validator
Returns a new instance of Validator.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sbsm/validator.rb', line 58 def initialize reset_errors() @boolean = self::class::BOOLEAN.dup @dates = self::class::DATES.dup @enums = self::class::ENUMS.dup @events = self::class::EVENTS.dup @files = self::class::FILES.dup @html = self::class::HTML.dup @numeric = self::class::NUMERIC.dup @patterns = self::class::PATTERNS.dup @strings = self::class::STRINGS.dup @uris = self::class::URIS.dup end |
Instance Method Details
#error? ⇒ Boolean
71 72 73 |
# File 'lib/sbsm/validator.rb', line 71 def error? !@errors.empty? end |
#perform_validation(key, value) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sbsm/validator.rb', line 124 def perform_validation(key, value) value = value.to_s.strip # require 'pry'; binding.pry if /event/.match(key.to_s) begin if(key==:event) symbol = value.to_sym symbol if @events.include?(symbol) elsif(@boolean.include?(key)) validate_boolean(key, value) elsif(@dates.include?(key)) validate_date(key, value) elsif(@enums.has_key?(key)) value if @enums[key].include?(value) elsif(@html.include?(key)) validate_html(value) elsif(@patterns.include?(key)) validate_pattern(key, value) elsif(@numeric.include?(key)) validate_numeric(key, value) elsif(@uris.include?(key)) validate_uri(key, value) elsif(@strings.include?(key)) validate_string(value) elsif(@files.include?(key)) StringIO.new(value) elsif(!Object.methods.include?(key) and self.respond_to?(key, true)) self.send(key, value) end rescue InvalidDataError => e SBSM.debug("#{e.key} #{e}") @errors.store(e.key, e) end end |
#reset_errors ⇒ Object
74 75 76 |
# File 'lib/sbsm/validator.rb', line 74 def reset_errors @errors = {} end |
#set_pass(value1, value2) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sbsm/validator.rb', line 77 def set_pass(value1, value2) valid1 = pass(value1.to_s) valid2 = pass(value2.to_s) if(value1.to_s.empty?) SBSM::InvalidDataError.new(:e_empty_pass, :pass, '') elsif(valid1 != valid2) SBSM::InvalidDataError.new(:e_non_matching_pass, :pass, '') else valid1 end end |
#valid_values(key) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/sbsm/validator.rb', line 88 def valid_values(key) key = key.intern if key.is_a? String @enums.fetch(key) { if(@boolean.include?(key)) ['false', 'true'] end } end |
#validate(key, value) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sbsm/validator.rb', line 96 def validate(key, value) value = value.pop if value.is_a? Array return nil if value.nil? # SBSM.info("validate #{key} #{value.class} #{value}") if value.is_a?(StringIO) if(@files.include?(key)) return validate_file(key, value) else begin value = value.read rescue StandardError => e p e end end elsif(value.is_a?(Array)) # TODO:: value = value[0] if(@files.include?(key)) return validate_file(key, value) else begin value = value.read rescue StandardError => e #p e end end end perform_validation(key, value) end |