Class: React::Validator
Instance Attribute Summary collapse
-
#errors ⇒ Object
writeonly
Sets the attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #all_other_params(name) ⇒ Object
- #build(&block) ⇒ Object
- #default_props ⇒ Object
-
#initialize(props_wrapper = Class.new(Component::PropsWrapper)) ⇒ Validator
constructor
A new instance of Validator.
- #optional(name, options = {}) ⇒ Object
- #requires(name, options = {}) ⇒ Object
- #validate(props) ⇒ Object
Constructor Details
#initialize(props_wrapper = Class.new(Component::PropsWrapper)) ⇒ Validator
Returns a new instance of Validator.
7 8 9 |
# File 'lib/react/validator.rb', line 7 def initialize(props_wrapper = Class.new(Component::PropsWrapper)) @props_wrapper = props_wrapper end |
Instance Attribute Details
#errors=(value) ⇒ Object
Sets the attribute errors
3 4 5 |
# File 'lib/react/validator.rb', line 3 def errors=(value) @errors = value end |
Class Method Details
.build(&block) ⇒ Object
11 12 13 |
# File 'lib/react/validator.rb', line 11 def self.build(&block) self.new.build(&block) end |
Instance Method Details
#all_other_params(name) ⇒ Object
30 31 32 33 |
# File 'lib/react/validator.rb', line 30 def all_other_params(name) @allow_undefined_props = true props_wrapper.define_all_others(name) { |props| props.reject { |name, value| rules[name] } } end |
#build(&block) ⇒ Object
15 16 17 18 |
# File 'lib/react/validator.rb', line 15 def build(&block) instance_eval(&block) self end |
#default_props ⇒ Object
47 48 49 50 51 |
# File 'lib/react/validator.rb', line 47 def default_props rules .select {|key, value| value.keys.include?("default") } .inject({}) {|memo, (k,v)| memo[k] = v[:default]; memo} end |
#optional(name, options = {}) ⇒ Object
25 26 27 28 |
# File 'lib/react/validator.rb', line 25 def optional(name, = {}) [:required] = false define_rule(name, ) end |
#requires(name, options = {}) ⇒ Object
20 21 22 23 |
# File 'lib/react/validator.rb', line 20 def requires(name, = {}) [:required] = true define_rule(name, ) end |
#validate(props) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/react/validator.rb', line 35 def validate(props) self.errors = [] validate_undefined(props) unless allow_undefined_props? props = coerce_native_hash_values(defined_props(props)) validate_required(props) props.each do |name, value| validate_types(name, value) validate_allowed(name, value) end errors end |