Class: React::Validator
Instance Attribute Summary collapse
-
#errors ⇒ Object
writeonly
Sets the attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #allow_undefined_props=(allow) ⇒ Object
- #build(&block) ⇒ Object
- #default_props ⇒ Object
-
#initialize(component_class) ⇒ Validator
constructor
A new instance of Validator.
- #optional(name, options = {}) ⇒ Object
- #requires(name, options = {}) ⇒ Object
- #undefined_props(props) ⇒ Object
- #validate(props) ⇒ Object
Constructor Details
#initialize(component_class) ⇒ Validator
Returns a new instance of Validator.
7 8 9 |
# File 'lib/react/validator.rb', line 7 def initialize(component_class) @component_class = component_class end |
Instance Attribute Details
#errors=(value) ⇒ Object
Sets the attribute errors
4 5 6 |
# File 'lib/react/validator.rb', line 4 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
#allow_undefined_props=(allow) ⇒ Object
30 31 32 |
# File 'lib/react/validator.rb', line 30 def allow_undefined_props=(allow) @allow_undefined_props = allow 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
51 52 53 54 55 |
# File 'lib/react/validator.rb', line 51 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 |
#undefined_props(props) ⇒ Object
34 35 36 37 |
# File 'lib/react/validator.rb', line 34 def undefined_props(props) self.allow_undefined_props = true props.reject { |name, value| rules[name] } end |
#validate(props) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/react/validator.rb', line 39 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 |